Search code examples
pythonmatplotlibwxpythontoolbarwxwidgets

Matplotlib and WxPython - Interactive Toolbar Plot not doing a Refresh/Update on click


I am trying to integrate matplotlib within a wxpython GUI, and I have gotten the interactive navigation toolbar to show up successfully. However, pressing any of the buttons does not change the plot, the plot only changes (based on the last pressed button) if you re-size the window or if you press another button (ie. zoom) and apply the relevant action (select a box/area). I tried looking online, but most of the answers either did not answer this specific issue, or the questions which seemed like they matched my query, were unanswered. Below is a segment of the relevant code:

Ignore all the numerous Update/Refresh calls, I was just trying to get the frame/plot to refresh, but it didn't work. Any ideas?

 class p1(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent, -1, size=(50,50))

            #Set up Figure
            self.figure = matplotlib.figure.Figure()
            self.canvas = FigureCanvas(self, -1, self.figure)

            #Set up Matplotlib Toolbar
            self.chart_toolbar = NavigationToolbar2Wx(self.canvas)
            tw,th = self.chart_toolbar.GetSizeTuple()
            fw,fh = self.canvas.GetSizeTuple()
            self.chart_toolbar.SetSize(wx.Size(fw, th))
            self.chart_toolbar.Realize()

            graphs_sizer = wx.BoxSizer(wx.VERTICAL)

            graphs_sizer.Add(self.canvas, 20, flag=wx.EXPAND, border=5)
            graphs_sizer.Add(self.chart_toolbar, 1, flag=wx.ALIGN_CENTER, border=5)

            self.SetSizer(graphs_sizer)
            self.chart_toolbar.update()
            self.canvas.Update()
            self.canvas.Refresh()
            self.Update()

        def plot(self):
            self.axes = self.figure.add_subplot(111)
            self.axes.plot(aggregatePlot,color='blue',alpha=0.6,lod=True)
       
            self.canvas.draw()
            self.canvas.Update()
            self.canvas.Refresh()

    class TestFrame(wx.Frame):
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, title=title, size=(200,200))

            #Create Splitter Window and Add Left/Right Panels
            self.sp = wx.SplitterWindow(self)
            self.p1 = p1(self.sp)
            self.p2 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
            self.sp.SplitVertically(self.p1, self.p2, 100)

            #Create Buttons
            self.siButton = wx.Button(self.p1, -1, "Si", size=(40,20), pos=(10,10))
            self.siButton.Bind(wx.EVT_BUTTON, self.Si)

            self.siButton = wx.Button(self.p2, -1, "No", size=(40,20), pos=(10,10))
            self.siButton.Bind(wx.EVT_BUTTON, self.No)

            #Create Status Bar
            self.statusbar = self.CreateStatusBar()
            self.statusbar.SetStatusText("Hola")

            #Plot
            self.p1.plot()

        #Event Handlers
        def Si(self, event):
            self.statusbar.SetStatusText("Si")

        def No(self, event):
            self.statusbar.SetStatusText("No")

    app = wx.App(redirect=False)
    frame = TestFrame(None, 'Hello World!')
    frame.Show(True)
    frame.p1.plot()
    frame.p1.canvas.Update()
    frame.p1.canvas.Refresh()
    app.MainLoop()

An example is shown below, the zoom button is pressed and a rectangular area for zoom is selected, but nothing happens. The zoom rectangle also has tons of little internal rectangles, but the actual graph doesn't change.

example of issue


Solution

  • An update to matplotlib just fixed my issue, not sure what the problem was but I found a bug online similar to the issue I was facing and it seemed to be internal. I just used conda to update my version of matplotlib, by running:

    conda update matplotlib
    

    Thanks!