Choosing a point in matplotlib embedded in wxPython

I’m trying to select and display data about a point in matplotlib embedded in wxPython.

I’ve written a minimal example which plots 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)

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

        panel.figure.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 I click a point, the data is correctly displayed. However, the next time I click a point, I receive an error. I tried searching for this error, but I wasn’t able to find any relevant threads. Thanks in advance for the 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?

backend_wx.py", line 1137, in _onLeftButtonDown
        self.CaptureMouse()

Makes me think A) it's a matplotlib bug

And

B) you probably can fix it by doing a releasemouse somewhere, like on mouseUp if here's a method like that somewhere.

see this for example:
https://github.com/crankycoder/wxPython-2.9.2.4/blob/master/wxPython/demo/ShapedWindow.py#L81

And here are the offending matplotlib lines:

  def _onLeftButtonDown(self, evt):
        """Start measuring on an axis."""
        x = evt.GetX()
        y = self.figure.bbox.height - evt.GetY()
        evt.Skip()
        self.CaptureMouse()
        FigureCanvasBase.button_press_event(self, x, y, 1, guiEvent=evt)

    def _onLeftButtonUp(self, evt):
        """End measuring on an axis."""
        x = evt.GetX()
        y = self.figure.bbox.height - evt.GetY()
        #print 'release button', 1
        evt.Skip()
        if self.HasCapture(): self.ReleaseMouse()

I wonder if it would be fixed if you moved the evt.Skip to after that last if statement?