Map dimensions change when highlighting a country with Geopandas - python

I create a world map using Geopandas and matplotlib in python. When I try to highlight a certain country, the map dimensions change. How can I preserve the map dimensions?
import matplotlib
from matplotlib import pyplot
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.collections import PatchCollection
from matplotlib.figure import Figure
import geopandas as gpd
import pandas
self.figure = Figure()
self.canvas = FigureCanvas(self, -1, self.figure)
self.axes = self.figure.add_axes([0, 0, 1, 1])
self.axes.margins(0.0)
self.world_data = gpd.read_file(WORLD)
self.axes.clear()
self.axes.axis('off')
self.figure.set_facecolor(WATER)
self.map_plot = self.world_data.to_crs(epsg=4326).plot(ax=self.axes, color=LAND)
if country_highlight:
self.world_data[self.world_data.ISO_A2_EH ==country_code].plot(edgecolor=u'gray', color='#fa8a48', ax=self.map_plot)
self.canvas.draw()
I attached a runnable below. Each time the user left clicks on the map, a random country is selected. But at each click, map dimensions change. What's wrong here? "ne_110m_admin_0_countries.shp" map file can be downloaded from: this page. You should put the sample code inside the map file directory.
import wx
import matplotlib
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import geopandas as gpd
import random
import pathlib
import os
this_directory = print(pathlib.Path(__file__).parent.resolve())
WORLDX = os.path.join(this_directory, "ne_110m_admin_0_countries.shp")
WATER = '#defafc'
LAND = '#807e7e'
LEFT_MOUSE = matplotlib.backend_bases.MouseButton.LEFT
class MapFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Map",
style=wx.DEFAULT_FRAME_STYLE,
size=wx.Size(1200, 900))
self.main_panel = MapPanel(self)
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, _event):
self.Destroy()
wx.Exit()
class MapPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.SetDoubleBuffered(True)
self.figure = Figure()
self.result_frame = parent
self.canvas = FigureCanvas(self, -1, self.figure)
self.axes = self.figure.add_axes([0, 0, 1, 1])
self.axes.margins(0.0)
self.axes.set_anchor('C')
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
self.Fit()
self.Layout()
self.countries = ["US", "FR", "GB", "BR", "IN", "AU", "NZ", "IT", "DE"]
self.gdf = None
self.canvas.mpl_connect('button_press_event', self.onLeftClick)
self.world_data = gpd.read_file(WORLDX)
self.map_plot = None
self.drawMap()
def drawMap(self, country_highlight=None):
"""Draw the map on the canvas. """
self.axes.clear()
self.axes.axis('off')
self.figure.set_facecolor(WATER)
self.map_plot = self.world_data.to_crs(epsg=4326).plot(ax=self.axes, color=LAND)
print("country highlight: ", country_highlight)
if country_highlight:
ylim = self.map_plot.get_ylim()
xlim = self.map_plot.get_xlim()
self.world_data[self.world_data.ISO_A2_EH == country_highlight].plot(edgecolor=u'gray', color='#fa8a48', ax=self.map_plot)
self.map_plot.set_ylim(*ylim)
self.map_plot.set_xlim(*xlim)
self.canvas.draw()
def onLeftClick(self, event):
if event.button == LEFT_MOUSE:
self.drawMap(random.choice(self.countries))
class MyApp(wx.App):
def OnInit(self):
frame = MapFrame()
frame.Show()
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()

If you want to hold the map extent for an axis constant after a certain point, you can save them and re-enforce them later:
# I'll drop references to `self` for the rest of the answer for clarity
ax = self.axes
world_data = self.world_data
world_data.to_crs(epsg=4326).plot(ax=ax, color=LAND)
ylim = ax.get_ylim()
xlim = ax.get_xlim()
if country_highlight:
self.world_data[
world_data.ISO_A2_EH == country_code
].plot(edgecolor=u'gray', color='#fa8a48', ax=ax)
ax.set_ylim(*ylim)
ax.set_xlim(*xlim)

Related

Update wxpython figure canvas with new figure

In a wxPython application I have an embedded a FigureCanvas with a matplotlib figure. I want to be able to switch the figure by loading a new one. However, the figure is not being updated.
Answers to similar topics suggests that panel.canvas.draw() and panel.Refresh() should do the trick, but I've also tried panel.Update() and panel.canvas.Refresh(). I fear that this only works if you want to redraw the canvas with the same figure?
So my question is: how do you replace the figure inside a canvas and make it update?
Below is a small (non-working) example. First a figure is loaded with a single axis. If you from the embedded shell type panel.LoadFigure() a new figure with 2x2 subplots is created and put into the canvas. But the new figure is not shown.
import numpy as np
import wx
from wx.py.shell import Shell
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import matplotlib.pyplot as plt
class ShellPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.shell = Shell(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.shell, 1, wx.GROW)
self.SetSizer(self.sizer)
self.Layout()
self.Fit()
class FigurePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.parent = parent
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.figure, ax = plt.subplots()
self.canvas = FigureCanvas(self, -1, self.figure)
self.shellpanel = ShellPanel(self)
s1 = wx.BoxSizer(wx.VERTICAL)
s1.Add(self.canvas, 0, wx.GROW)
s1.Add(self.shellpanel, 1 , wx.EXPAND)
self.sizer.Add(s1, 5, wx.GROW)
self.SetSizer(self.sizer)
self.Layout()
self.Fit()
def LoadFigure(self):
self.figure, ax = plt.subplots(2, 2)
self.canvas.draw()
self.Refresh()
class FigureFrame(wx.Frame):
def __init__(self, parent, id, title, size):
wx.Frame.__init__(self, parent, id, title, size=size)
if __name__ == "__main__":
app = wx.App(False)
fr = FigureFrame(None, -1, title='Figure Loader', size=(wx.DisplaySize()[0]/2, wx.DisplaySize()[1]*3/4))
panel = FigurePanel(fr)
fr.Show()
app.MainLoop()
Is there any reason you are trying to create a new Figure and not simply new Axes on the already defined Figure?
I would write your code like so:
def LoadFigure(self):
self.figure.clf() # clear current figure
# add arbitrary number of new Axes
self.figure.add_subplot(221)
self.figure.add_subplot(222)
self.figure.add_subplot(223)
self.figure.add_subplot(224)
self.canvas.draw() # refresh canvas
EDIT: following your comment, I think the problem is that you're creating a new figure, but your Canvas is still referencing the old one. I don't know if you can change that directly in the FigureCanvas properties, maybe someone with more experience can provide a better answer. For the moment, I would Destroy the previous canvas, and create a new FigureCanvas object with your new figure.
def LoadFigure(self):
self.figure, ax = plt.subplots(2, 2)
self.canvas.Destroy()
self.canvas = FigureCanvas(self, -1, self.figure)
self.canvas.draw()
Try using the Figure itself to make your figure, not pyplot. It has usually worked for me because it gives you more room to improvise.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import wx
from wx.py.shell import Shell
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import matplotlib.pyplot as plt
class ShellPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.shell = Shell(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.shell, 1, wx.GROW)
self.SetSizer(self.sizer)
self.Layout()
self.Fit()
class FigurePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.parent = parent
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.figure = Figure(figsize = (8,6.1), dpi =60)
self.ax = self.figure.add_subplot(1,1,1)
self.enlarged_figure = Figure(figsize = (8,6.1), dpi = 100)
self.ax2 = self.enlarged_figure.add_subplot(2,1,2)
self.canvas = FigureCanvas(self, -1, self.figure)
self.canvas.Show()
self.enlarged_canvas = FigureCanvas(self,-1,self.enlarged_figure)
self.enlarged_canvas.Show()
self.shellpanel = ShellPanel(self)
s1 = wx.BoxSizer(wx.VERTICAL)
s1.Add(self.canvas, 0, wx.GROW)
s1.Add(self.shellpanel, 1 , wx.EXPAND)
self.sizer.Add(s1, 5, wx.GROW)
self.SetSizer(self.sizer)
self.Layout()
self.Fit()
def LoadFigure(self):
self.figure, ax = plt.subplots(2, 2)
self.canvas.draw()
self.Refresh()
class FigureFrame(wx.Frame):
def __init__(self, parent, id, title, size):
wx.Frame.__init__(self, parent, id, title, size=size)
if __name__ == "__main__":
app = wx.App(False)
fr = FigureFrame(None, -1, title='Figure Loader', size=(wx.DisplaySize()[0]/2, wx.DisplaySize()[1]*3/4))
panel = FigurePanel(fr)
fr.Show()
app.MainLoop()
In the shell type in 'panel.LoadFigure()' and the new figure will show up very quickly and then disappear. Then type 'panel.canvas.draw()' and the new figure will be there. Then type 'panel.LoadFigure()' to load the old figure back. Repeat.
I don't know why the figure remains hidden after the MainLoop() continues but this is sort of a quick fix to your problem.

plotting date series with wxpython and matplotlib - x and y must have same first dimension

I am trying to plot some date series data. As soon as I add the dates I get an error. It works fine if I replace them with numbers. What am I doing wrong?
My code:
import numpy
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import wx
class CanvasPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
self.Fit()
def draw(self):
t = ['1/1/2013','1/2/2013','1/3/2013','1/4/2013','1/5/2013','1/6/2013',
'1/7/2013','1/8/2013','1/9/2013']
s = [100,121,89,111,343,211,151,232,122,343]
self.axes.plot(t, s)
self.tittle1 = wx.StaticText(self, label="My Label")
if __name__ == "__main__":
app = wx.PySimpleApp()
fr = wx.Frame(None, title='My Data')
panel = CanvasPanel(fr)
panel.draw()
fr.Show()
app.MainLoop()
Error:
ValueError: x and y must have same first dimension
In [1]: t = ['1/1/2013','1/2/2013','1/3/2013','1/4/2013','1/5/2013','1/6/2013','1/7/2013','1/8/2013','1/9/2013']
In [2]: s = [100,121,89,111,343,211,151,232,122,343]
In [3]: len(t)
Out[3]: 9
In [4]: len(s)
Out[4]: 10

WXPython with MatPlotLib

I am trying to use MatPlotLib with WXPython. I find a nice example from http://www.cs.colorado.edu/~kena/classes/5448/s11/presentations/pearse.pdf
because it show how to use 2 panels and it is well explained how to embed matplotlib. But I think it is missing some part, because the graphic isn't being initialized.
How can I fix that?
import wx
import numpy
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class TestFrame(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(500,500))
self.sp = wx.SplitterWindow(self)
self.p1 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
self.p2 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
self.sp.SplitVertically(self.p1,self.p2,100)
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText('Oi')
class p1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent,-1,size=(50,50))
self.figure = matplotlib.figure.Figure()
self.axes = self.figure.add_subplot(111)
t = numpy.arange(0.0,10,1.0)
s = [0,1,0,1,0,2,1,2,1,0]
self.y_max = 1.0
self.axes,plot(t,s)
self.canvas = FigureCanvas(self,-1,self.figure)
app = wx.App(redirect=False)
frame = TestFrame(None, 'Hello World!')
frame.Show()
app.MainLoop()
Thanks.
You made class p1 with matplot but you didn't use it in TestFrame.
I changed some names to make it more clear
import wx
import numpy
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class TestFrame(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(500,500))
self.sp = wx.SplitterWindow(self)
self.p1 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
self.p2 = MatplotPanel(self.sp)
self.sp.SplitVertically(self.p1,self.p2,100)
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText('Oi')
class MatplotPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent,-1,size=(50,50))
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
t = numpy.arange(0.0,10,1.0)
s = [0,1,0,1,0,2,1,2,1,0]
self.y_max = 1.0
self.axes.plot(t,s)
self.canvas = FigureCanvas(self,-1,self.figure)
app = wx.App(redirect=False)
frame = TestFrame(None, 'Hello World!')
frame.Show()
app.MainLoop()
see also (as example) my answer to how to combine wxPython, matplotlib and Pyopengl
EDIT:
MatplotPanel with NavigationToolbar and wx.Button to change plot.
EDIT: 2022.01.03
As #Samuel said in comment: newer matplotlib needs NavigationToolbar2WxAgg instead of NavigationToolbar2Wx. See also matplotlib doc: Embedding in wx #2
# older matplotlib
#from matplotlib.backends.backend_wxagg import NavigationToolbar2Wx as NavigationToolbar
# newer matplotlib
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar
class MatplotPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent,-1,size=(50,50))
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.sizer)
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.toolbar = NavigationToolbar(self.canvas)
self.button = wx.Button(self, -1, "Change plot")
self.button.Bind(wx.EVT_BUTTON, self.changePlot)
self.sizer.Add(self.toolbar, 0, wx.EXPAND)
self.sizer.Add(self.button, 0, wx.EXPAND)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.drawSin()
self.current_draw = 'sin'
# self.drawLines()
def changePlot(self, event):
if self.current_draw == 'sin' :
self.drawLines()
self.current_draw = 'lines'
else:
self.drawSin()
self.current_draw = 'sin'
self.Layout()
def drawLines(self):
x = numpy.arange(0.0,10,1.0)
y = [0,1,0,1,0,2,1,2,1,0]
self.axes.clear()
self.axes.plot(x, y)
def drawSin(self):
x = numpy.arange(0.0,10,0.1)
y = numpy.sin(x)
self.axes.clear()
self.axes.plot(x, y)

selecting points from a graph matplotlib

At the moment I have a scatter graph which I can zoom in, move around etc.
What I also want to do is be able to select a certain number of points on the graph, then store the selected points in an array.
Is there any special function in matplotlib I can use?
Any help will be greatly appreciated
My code
import os
import wx
import numpy as nump
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.figure as fg
import matplotlib.backends.backend_wxagg as wxagg
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Title')
self.create_main_panel()
self.draw_figure()
def create_main_panel(self):
self.panel = wx.Panel(self)
self.dpi = 100
self.fig = fg.Figure((5.0, 4.0), dpi=self.dpi)
self.canvas = wxagg.FigureCanvasWxAgg(self.panel, -1, self.fig)
self.axes = self.fig.add_subplot(111)
self.toolbar = wxagg.NavigationToolbar2WxAgg(self.canvas)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.vbox.AddSpacer(25)
self.vbox.Add(self.toolbar, 0, wx.EXPAND)
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
def draw_figure(self):
self.axes.clear()
x, y = [2,3,4,5]
self.axes.scatter(x, y)
self.canvas.draw()
def on_exit(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = MyFrame()
app.frame.Show()
app.MainLoop()
The code below provides a possible solution. The basic method may be summarized as follows:
One attaches a handler for pick_event that keeps appending the picked data indices to a list self._picked_indices.
A key_press_event handler clears the self._picked_indices list whenever the user presses the escape key.
The method MyFrame.picked_points returns a list of coordinates of currently selected points. This method returns None if no points have been selected yet (you can modify it to return an empty list in this case if that is more convenient).
This way you can keep selecting points by clicking on them. But if you want to start over again, just press escape and start picking again.
#! /usr/bin/env python
import os
import wx
import numpy as nump
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.figure as fg
import matplotlib.backends.backend_wxagg as wxagg
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Title')
self.create_main_panel()
self.draw_figure()
self._is_pick_started = False
self._picked_indices = None
def create_main_panel(self):
self.panel = wx.Panel(self)
self.dpi = 100
self.fig = fg.Figure((5.0, 4.0), dpi=self.dpi)
self.canvas = wxagg.FigureCanvasWxAgg(self.panel, -1, self.fig)
self.axes = self.fig.add_subplot(111)
self.toolbar = wxagg.NavigationToolbar2WxAgg(self.canvas)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.vbox.AddSpacer(25)
self.vbox.Add(self.toolbar, 0, wx.EXPAND)
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
self.fig.canvas.mpl_connect('pick_event', self.on_pick)
self.fig.canvas.mpl_connect('key_press_event', self.on_key)
def draw_figure(self):
self.axes.clear()
self._x_data, self._y_data = [[2,3], [4,5]]
self.axes.scatter(self._x_data, self._y_data, picker=5)
self.canvas.draw()
def on_exit(self, event):
self.Destroy()
def picked_points(self):
if self._picked_indices is None:
return None
else:
return [ [self._x_data[i], self._y_data[i]]
for i in self._picked_indices ]
def on_pick(self, event):
if not self._is_pick_started:
self._picked_indices = []
self._is_pick_started = True
for index in event.ind:
if index not in self._picked_indices:
self._picked_indices.append(index)
print self.picked_points()
def on_key(self, event):
"""If the user presses the Escape key then stop picking points and
reset the list of picked points."""
if 'escape' == event.key:
self._is_pick_started = False
self._picked_indices = None
return
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = MyFrame()
app.frame.Show()
app.MainLoop()

Matplotlib: Label points on mouseover

I have a scatter plot with several thousand points. This post tells me how to label them:
Matplotlib: How to put individual tags for a scatter plot
But that will look like a disaster with so many points. What I would like instead is to have a "tool tip" type label that pops up when you mouseover a point. Is that possible using matplotlib?
Once you get the coords of the point you can show them or any object-linked info in a textctrl in the toolbar. For this you have to instantiate a toolbar (NavigationToolbar2Wx()) in your canvas and add the textcontrol there. This is not as nice as a popup but it does the job.
Here you have an example of customizing your toolbar (only showing the x coordinate in the txtctrl):
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
"""
jvisor_spectrum_panel (visor_07)
25 julio 2010
"""
#
import wx
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
#
#
class SpectrumPanel(wx.Panel):
def __init__(self, parent, xlabel='m/z', ylabel='Intensity'):
wx.Panel.__init__(self, parent)
#
self.parent = parent
self.xlabel = xlabel
self.ylabel = ylabel
self.SetBackgroundColour("white")
#
self.figure = Figure()
self.canvas = FigureCanvas(self, -1, self.figure)
#
self.add_toolbar()
#
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP| wx.GROW| wx.EXPAND)
sizer.Add(self.toolbar, 0, wx.LEFT)
self.canvas.mpl_connect('motion_notify_event', self.on_motion)
self.SetSizer(sizer)
self.Fit()
self.clean()
#
def add_toolbar(self):
""
self.toolbar = NavigationToolbar2Wx(self.canvas)
mass_txt = wx.StaticText(self.toolbar, label='m/z', pos=(230, 7),
size=(25, 17))
mass_txt.SetBackgroundColour("light gray")
self.mass = wx.TextCtrl(self.toolbar, pos=(260,4), size=(50, 22),
style=wx.TE_READONLY)
#
self.toolbar.SetToolBitmapSize(wx.Size(24, 25))
self.toolbar.SetMinSize((1500, 31))
self.toolbar.Realize()
self.toolbar.Update()
#
def clean(self):
""
self.figure.clear()
self.axes = self.figure.add_subplot(111)
#
def dibuja(self):
"dibuja el canvas"
self.axes.set_xlabel(self.xlabel)
self.axes.set_ylabel(self.ylabel)
self.canvas.draw()
#
def on_motion(self, evt):
if evt.inaxes:
xpos = evt.xdata
self.mass.SetValue(' %0.1f' % (xpos))
if __name__ == '__main__':
""
class TestFrame(wx.Frame):
def __init__(self, *args, **kargs):
wx.Frame.__init__(self, *args, **kargs)
self.panel = SpectrumPanel(self)
self.Fit()
#
app = wx.PySimpleApp()
fr = TestFrame(None)
fr.Show()
app.MainLoop()
And here you can see the new control in the toolbar:

Categories

Resources