0

I've encountered this behaviour intermittently using the Matplotlib NavigationToolbar2Wx in a Matplotlib Figure canvas in a wx.Frame (or wx.Panel). If the Zoom Icon or Pan Icon are selected the icon disappears however a click in the vacant space still toggles the tool. The Icons for the Home, Backward step or Forward step all behave as expected.

Can anyone offer advice on 1. what causes it and 2. how to fix it?

Thanks to joaquin for posting the initial code slightly modified to include the toolbar. (http://stackoverflow.com/questions/10737459/embedding-a-matplotlib-figure-inside-a-wxpython-panel)

I'm use python 2.6, wxPython 2.9.2.4 osx-carbon (classic) and Matplotlib 1.1.0

Thanks

The code below shows the problem:

    #!/usr/bin/env python
    # encoding: UTF-8
    """
    wxPython and Matplotlib Canvas with Matplotlib Toolbar.py

    """
    from numpy import arange, sin, pi
    import matplotlib
    matplotlib.use('WXAgg')

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

    import wx

    class CanvasPanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
            self.figure = Figure()
            self.axes = self.figure.add_subplot(111)
            self.canvas = FigureCanvas(self, -1, self.figure)
            # Add Matplotlib Toolbar
            # Add the Matplotlib Navigation toolBar here
            self.toolbar=NavigationToolbar2Wx(self.canvas)
            self.toolbar.AddLabelTool(5,'',wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, (32,32)))
            #self.Bind(wx.EVT_TOOL, self.NewTitle(), id=5)
            self.toolbar.Realize()      

            # Add to Box Sizer
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.TOP | wx.GROW)
            self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            self.SetSizer(self.sizer)
            self.Fit()

        def draw(self):
            t = arange(0.0, 3.0, 0.01)
            s = sin(2 * pi * t)
            self.axes.plot(t, s)


    if __name__ == "__main__":
        app = wx.PySimpleApp()
        fr = wx.Frame(None, title='test',size=(800,600))
        panel = CanvasPanel(fr)
        panel.draw()
        fr.Show()
        app.MainLoop()
user1032355
  • 335
  • 1
  • 8
  • 13

1 Answers1

0

I can't comment on the causes of this specific issue, but I was experiencing some problems with non-Agg backend with wxpython 2.9 too (while the code worked ok in 2.8). Replacing the Toolbar with the Agg version fixed such problems for me; e.g.:

from matplotlib.backends.backend_wx import NavigationToolbar2Wx

==>

from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg

and adjusting the code accordingly:

self.toolbar=NavigationToolbar2Wx(self.canvas)

==>

self.toolbar = NavigationToolbar2WxAgg(self.canvas)

hth, vbr

vbr
  • 1
  • I've tried using both NavigationToolbar2Wx and NavigationToolbar2WxAgg with the same result disappearing icons for the Zoom and Pan. I found one other similar problem where the NavigationToolbar2 was hidden and a new toolbar was constructed using the same calls which seemed to work when the toolbar was set in the Frame. I haven't tried to put it in the figure canvas to see if it works. – user1032355 Aug 05 '12 at 05:51
  • It looks like the problem is due to adding new bitmap buttons to the NavigationToolbar2Wx toolbar. If I remove the added tools the problem goes away. I guess I'll have to create another toolbar with the additional tools – user1032355 Aug 06 '12 at 16:19