选择嵌入在wxPython中的matplotlib中的一个点

时间:2014-08-15 18:39:46

标签: python matplotlib wxpython

我试图选择并显示有关wxPython中嵌入的matplotlib中某个点的数据。

我写了一个绘制随机数据的最小例子。代码如下。

import numpy as np
import wx


from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

class PlotGUI(wx.Frame):
    """Class to display basic GUI elements."""
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        panel = wx.Panel(self)
        self.panel = panel

        vert_sizer = wx.BoxSizer(wx.VERTICAL)
        self.vert_sizer = vert_sizer

        panel.figure = Figure()
        panel.canvas = FigureCanvas(panel, -1, panel.figure)
        self.panel.canvas = panel.canvas

        panel.axes = panel.figure.add_subplot(111)
        self.panel.axes = panel.axes

        vert_sizer.Add(panel.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
        panel.SetSizer(vert_sizer)
        panel.Fit()

        self.plot_data()

        self.panel.canvas.mpl_connect('pick_event', self.display_data)

    def display_data(self, event):
        wx.MessageBox('x :'+str(event.mouseevent.xdata) + 'y: ' + str(event.mouseevent.ydata), 'Info',wx.OK | wx.ICON_INFORMATION)

    def plot_data(self):
        x = np.arange(10)
        y = np.random.randn(10)
        self.panel.axes.plot(x,y, 'o', picker = 5)

def main():
    app = wx.App()
    GUI = PlotGUI(None)
    GUI.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

第一次点击某个点时,数据会正确显示。但是,下次单击某个点时,我收到错误。我尝试搜索此错误,但我无法找到任何相关的主题。在此先感谢您的帮助。

Traceback (most recent call last):
  File "/home/pythontology/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_wx.py", line 1137, in _onLeftButtonDown
    self.CaptureMouse()
  File "/home/pythontology/anaconda/lib/python2.7/site-packages/wx-3.0-gtk2/wx/_core.py", line 10641, in CaptureMouse
    return _core_.Window_CaptureMouse(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "!wxMouseCapture::IsInCaptureStack(this)" failed at ./src/common/wincmn.cpp(3271) in CaptureMouse(): Recapturing the mouse in the same window?

1 个答案:

答案 0 :(得分:3)

经过大量搜索, tcaswell 推荐this thread。读完之后,我发现在wx.MessageBox之前添加行if self.panel.canvas.HasCapture(): self.panel.canvas.ReleaseMouse()修复了问题。