弹出注解内的wxPython matplotlib(Pop up annotations on ma

2019-07-30 02:17发布

我有两个小组的wxPython GUI设置。 在我的右侧面板中,我使用底图有地图显示。 在此底图(美国的)我绘制不同城市的散点图。 我希望能够点击这些点,有提供了一些相对于点我选择信息我的GUI中的弹出窗口(如城市,纬度/长等 - 我不得不保存了所有信息以列表或其它装置)。

我所遇到AnnoteFinder,但这似乎并没有给我的GUI内工作(如果我在2面板GUI使用底图通过itelf,而不是它的工作)。 而且,这只是穿上圆点的顶部一些文字 - 我宁愿有一个小窗口显示出来。

我的代码示例至今:

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

有什么想法吗?

Answer 1:

看看这个答案 。 基本上你设置了创建的图表注释一挑事件。 该注解可弹出的工具提示样式的文本框中。

请注意,这不会产生真正的GUI“窗口”(即,一个对话框或其他控制与关闭按钮,标题栏等),但只是对情节本身的注释。 然而,从看代码,你可以看到它是如何确定你所点击的艺术家(如,点)。 一旦你的信息,你可以运行任何你想要的代码吧,比如创建wxPython的对话,而不是一个注释。

编辑重新您对最后几行的问题:基于您的代码,它看起来像你想要做的:

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 ,不同类型的艺术家(即,不同类型的地块)将提供不同的事件信息。

我有一些旧的代码,做几乎完全你所描述的(点击地图上的一个点时,显示城市名)。 我不得不承认,我不记得究竟如何这一切工作,但我的代码有这个在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得到相应拾取点的城市名称。 您的代码可能需要根据您的数据的格式稍有不同,但应该给你一个想法。



文章来源: Pop up annotations on matplotlib within wxPython