在 wxPython 中的 matplotlib 上弹出注释 [英] Pop up annotations on matplotlib within wxPython

查看:43
本文介绍了在 wxPython 中的 matplotlib 上弹出注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了两个面板 wxPython GUI.在我的右侧面板中,我有一个使用 Basemap 的地图显示.在这张(美国)底图上,我绘制了不同城市的散点图.我希望能够点击这些点并在我的 GUI 中弹出一个窗口,该窗口提供与我选择的那个点相关的一些信息(例如城市、纬度/经度等 - 我会存储所有这些信息以列表或其他方式).

I have a two panel wxPython GUI set up. In my right panel, I have a map display using Basemap. On this basemap (of the United States) I plot a scatter plot of different cities. I would like to be able to click on these dots and have a pop up window within my GUI that gives some information relative to that dot I select (ex. City, lat/long, etc. -- I would have all this info stored in a list or other means).

我遇到过 AnnoteFinder,但这似乎在我的 GUI 中不起作用(如果我通过 itelf 使用 Basemap 而不是在我的 2 面板 GUI 中,它将起作用).此外,这只是将一些文本放在点的顶部——我宁愿显示一个小窗口.

I have come across AnnoteFinder, but this does not seem to work inside my GUI (it will work if i use Basemap by itelf and not in my 2 panel GUI). Also, this just puts some text on top of the dot -- I would rather have a small window show up.

到目前为止我的代码示例:

Example of my code so far:

#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()

有什么想法吗?

推荐答案

查看 这个答案.基本上,您设置了一个在图表上创建注释的选择事件.此注释可以作为工具提示样式的文本框弹出.

Check out this answer. Basically you set up a pick event that creates an annotation on the graph. This annotation can pop up as a tooltip-style text box.

请注意,这不会产生真正的 GUI窗口"(即对话框或其他带有关闭按钮、标题栏等的控件),而只是绘图本身的注释.但是,通过查看代码,您可以看到它如何确定您单击的艺术家(例如,点).获得该信息后,您可以使用它运行任何您想要的代码,例如创建一个 wxPython 对话框而不是注释.

Note that this doesn't produce a real GUI "window" (i.e., a dialog box or other control with close button, title bar, etc.), but just an annotation on the plot itself. However, from looking at the code you can see how it determines the artist (e.g., point) you've clicked on. Once you have that info, you could run whatever code you want with it, for instance creating a wxPython dialog instead of an annotation.

编辑是关于最后几行的问题:根据您的代码,您似乎想要这样做:

Edit re your question about the last few lines: Based on your code, it looks like you'd want to do:

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

另一个编辑是关于在注释中使用不同文本的问题:您可能需要稍微处理一下事件对象以提取您想要的信息.如 http://matplotlib.sourceforge.net/users/event_handling 所述.html#simple-picking-example,不同的艺术家类型(即不同类型的情节)将提供不同的事件信息.

Another edit re your question about having different text in the annotation: You may have to play around with the event object a bit to extract the information you want. As described at http://matplotlib.sourceforge.net/users/event_handling.html#simple-picking-example , different artist types (i.e., different kinds of plots) will provide different event information.

我有一些旧代码,几乎完全符合您的描述(单击地图上的某个点时显示城市名称).我不得不承认我不记得它到底是如何工作的,但是我的代码在 DataCursor 中有这个:

I have some old code that does almost exactly what you described (displaying city name when a point on the map is clicked). I have to admit I don't remember exactly how all of it works, but my code has this in the 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'] 是城市名称列表,我能够使用 event.ind 索引到该列表中以获取与城市名称相对应的城市名称选取的点.根据您的数据格式,您的代码可能需要略有不同,但这应该能让您有所了解.

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

这篇关于在 wxPython 中的 matplotlib 上弹出注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆