Point selection in matplotlib embedded in wxPython

I am trying to select and display point data in matplotlib embedded in wxPython.

I wrote a minimal example that displays random data. The code is below.

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

      

The first time you click on a point, the data is displayed correctly. However, the next time I hit period, I get an error. I tried to find this error, but I could not find any related topics. Thanks in advance for your help.

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?

      

+3


source to share


1 answer


After a lot of searching for tcaswell , this thread is recommended . After reading this I found that adding the line if self.panel.canvas.HasCapture(): self.panel.canvas.ReleaseMouse()

before the wx.MessageBox fixed the problem.



+2


source







All Articles