弹出注解内的wxPython matplotlib [英] Pop up annotations on matplotlib within wxPython

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

问题描述

我有两个小组的wxPython GUI设置。在我的右侧面板中,我使用底图有地图显示。在这个底图(美国)我绘制不同城市的散点图。我希望能够点击这些点,有提供了一些相对于该点我选择我的信息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在我的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.

我的code的例子至今:

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

有什么想法?

推荐答案

查看<一个href=\"http://stackoverflow.com/questions/4652439/is-there-a-matplotlib-equivalent-of-matlabs-datacursormode\">this回答。基本上你设置了创建的图表注释一挑的事件。这个注解可以弹出一个提示风格的文本框。

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的窗口(即一个对话框或者其他控制与关闭按钮,标题栏等),但只是对剧情本身的注释。然而,从看code你可以看到它是如何确定的艺术家(例如,点),你点击。一旦你的信息,你可以c您想用它,比如创建wxPython的对话,而不是注释的运行无论$ C $。

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.

修改重新您对最后几行的问题:根据您的code,它看起来像你想要做的:

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.

我有一些旧code,做几乎完全你所描述的(点击地图上的一个点时,显示城市名称)。我不得不承认,我不记得究竟是如何的所有它的工作原理,但我的code有这样的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 ['名称'] 是城市名的列表中,我能够索引,使用 event.ind 来获得对应的采摘点的城市名称。您code可能需要根据您的数据的格式稍有不同,但应该给你一个想法。

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天全站免登陆