Popup annotations on matplotlib in wxPython

I have two panel wxPython GUIs. In my right pane, I have a map display using Basemap. On this base map (USA) I draw the scatter for different cities. I would like to be able to click on these points and have a pop-up in my GUI that gives some information regarding this point that I select (e.g. City, lat / long, etc.). I would save all this information in a list or other means).

I came across AnnoteFinder, but this does not seem to work inside my GUI (it will work if I use Basemap on my own, and not in my 2-panel GUI). Also, it just puts some text on top of the dot - I would prefer a small window to appear.

An example of my code:

#Setting up Map Figure self.figure = Figure(None,dpi=75) self.canvas = FigureCanvas(self.PlotPanel, -1, self.figure) self.axes = self.figure.add_axes([0,0,1,1],frameon=False) self.SetColor( (255,255,255) ) #Basemap Setup self.map = Basemap(llcrnrlon=-119, llcrnrlat=22, urcrnrlon=-64, urcrnrlat=49, projection='lcc', lat_1=33, lat_2=45, lon_0=-95, resolution='h', area_thresh=10000,ax=self.axes) self.map.drawcoastlines() self.map.drawcountries() self.map.drawstates() self.figure.canvas.draw() #Set up Scatter Plot m = Basemap(llcrnrlon=-119, llcrnrlat=22, urcrnrlon=-64, urcrnrlat=49, projection='lcc', lat_1=33, lat_2=45, lon_0=-95, resolution='h', area_thresh=10000,ax=self.axes) x,y=m(Long,Lat) #Scatter Plot (they plot the same thing) self.map.plot(x,y,'ro') self.map.scatter(x,y,90) self.figure.canvas.draw() 

Any thoughts?

+1
source share
1 answer

Mark this answer . Basically, you set up a selection event that creates an annotation on the chart. This annotation may appear as a text field in a tooltip style.

Please note that this does not create a real β€œwindow” for the graphical interface (ie a dialog box or other control with a close button, title, etc.), but simply annotations on the plot itself. However, looking at the code, you can see how it identifies the artist (for example, the point) that you clicked on. Once you have this information, you can run any code you need, for example, by creating a wxPython dialog instead of an annotation.

Edit the question about the last few lines: based on your code, it looks like you want:

 pts = self.map.scatter(x, y, 90) self.figure.canvas.mpl_connect('pick_event', DataCursor(plt.gca())) pts.set_picker(5) 

Another editing option is the question of whether there is another text in the annotation. You may need to play a little with the event object to extract the information you need. As described in http://matplotlib.sourceforge.net/users/event_handling.html#simple-picking-example , different types of artists (i.e. different kinds of charts) will provide information about different events.

I have an old code that does almost what you described (displaying the name of the city when you click a point on the map). I have to admit that I don’t remember exactly how it all works, but my code has this in a DataCursor:

 def __call__(self, event): self.event = event xdata, ydata = event.artist._offsets[:,0], event.artist._offsets[:,1] #self.x, self.y = xdata[event.ind], ydata[event.ind] self.x, self.y = event.mouseevent.xdata, event.mouseevent.ydata if self.x is not None: city = clim['Name'][event.ind[0]] if city == self.annotation.get_text() and self.annotation.get_visible(): # You can click the visible annotation to remove it self.annotation.set_visible(False) event.canvas.draw() return self.annotation.xy = self.x, self.y self.annotation.set_text(city) self.annotation.set_visible(True) event.canvas.draw() 

clim['Name'] is a list of city names, and I was able to index it using event.ind to get the city name corresponding to the selected point. Your code may be slightly different depending on the format of your data, but this should give you an idea.

+2
source

All Articles