matplotlib Embedded in wxPython with Navigation Toolbar Coordinates

Refresh

last month

Views

64 time

1

I've come across and implemented a few scripts with matplotlib figures embedded in a wxPython panel. The embedding of the actual plot is fine but when I add a navigation toolbar NavigationToolbar2WxAgg much of the toolbar functionality is lost. I can pan and zoom, but there are no coordinates displayed, and the default shortcut keys do not work. The same behavior occurs in embedding_in_wx4_sgskip.py from the example/user_interfaces folder for matplotlib:

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx as NavigationToolbar
from matplotlib.figure import Figure

import numpy as np
import matplotlib as mpl

import wx
import wx.lib.mixins.inspection as WIT


class CanvasFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,
                      'CanvasFrame', size=(550, 350))

        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        t = np.arange(0.0, 3.0, 0.01)
        s = np.sin(2 * np.pi * t)

        self.axes.fmt_xdata = lambda x: "{0:f}".format(x)
        self.axes.fmt_ydata = lambda x: "{0:f}".format(x)        

        self.axes.plot(t, s)
        self.canvas = FigureCanvas(self, -1, self.figure)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
        self.SetSizer(self.sizer)
        self.Fit()
        self.add_toolbar()  # comment this out for no toolbar

    def add_toolbar(self):
        self.toolbar = NavigationToolbar(self.canvas)
        self.toolbar.Realize()
        # By adding toolbar in sizer, we are able to put it at the bottom
        # of the frame - so appearance is closer to GTK version.
        self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
        # update the axes menu on the toolbar
        self.toolbar.update()


# alternatively you could use
#class App(wx.App):
class App(WIT.InspectableApp):
    def OnInit(self):
        'Create the main window and insert the custom frame'
        self.Init()
        frame = CanvasFrame()
        frame.Show(True)

        return True

app = App(0)
app.MainLoop()

How do I restore or add this functionality to my navigation bar?

1 answers

0

You can put this code here:

    #Create 'Position Display'
    self.Text = wx.StaticText( self, wx.ID_ANY, u"  Available Channels  ", wx.DefaultPosition, wx.DefaultSize, 0 )
    self.Text.Wrap( -1 )
    mouseMoveID = self.canvas.mpl_connect('motion_notify_event',
                                                 self.onMotion)

Before you create your sizer. Then add this to the end of your init definition:

    self.sizer.Add(self.Text,0, wx.LEFT | wx.EXPAND)

Finally, add this function to capture mouse movement on the frame:

def onMotion(self, evt):
    """This is a bind event for the mouse moving on the MatPlotLib graph
        screen. It will give the x,y coordinates of the mouse pointer.
    """
    xdata = evt.xdata
    ydata = evt.ydata
    try:
        x = round(xdata,4)
        y = round(ydata,4)
    except:
        x = ""
        y = ""

    self.Text.SetLabelText("%s (s), %s" % (x,y))

This is what worked for me, good luck!