3

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

1 Answer 1

3

After a lot of searching, tcaswell recommended this thread. After reading this, I found that adding the line if self.panel.canvas.HasCapture(): self.panel.canvas.ReleaseMouse() before the wx.MessageBox fixed the issue.

Sign up to request clarification or add additional context in comments.

1 Comment

It would be great if you could this up as an example to go in the mpl docs in the UI embedding example folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.