Search code examples
pythonmatplotlibwxpythonwxmpl

Error when updating wxmplot graph


I am trying to create a graph that updates dynamically using wxmplot in Python 3.6. According to the documentation here: http://cars.uchicago.edu/software/python/wxmplot/plotpanel.html#plotpanel.plot, I should be calling the update_line function which allows for faster graph updates compared to redrawing the plot. However, this function is not working for me. Here is my code:

    import wx
    import random
    from wxmplot import PlotPanel

    class MainFrame(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, None, size=(1200, 900))

            self.panel_1 = Panel_one(self)
            self.button_1 = wx.Button(self, label='update graph', size=(100, 30))
            self.Bind(wx.EVT_BUTTON, self.click_button, self.button_1)
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.panel_1, 0, wx.LEFT, 5)
            sizer.Add(self.button_1, 0, wx.LEFT, 5)
            self.SetSizer(sizer)

        def click_button(self, e):
            x.append(max(x)+1)
            y.append(random.randint(0, 10))
            self.panel_1.graph1.update_line(max(x), x, y)

    class Panel_one(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent, -1, style=wx.SUNKEN_BORDER)

            self.graph1 = PlotPanel(self, size=(400, 400))
            self.graph1.plot(x, y)


    x = [0, 1, 2]
    y = [5, 8, 4]

    if __name__ == "__main__":
        app = wx.App(redirect=False)
        frame = MainFrame(None)
        frame.Show()
        app.MainLoop()

The idea is for the graph to update whenever the button is clicked with a new data point randomly being generated. I get an error that says: AttributeError: 'list' object has no attribute 'min'. I am not sure what I did wrong, but I think it may have to do with the update_line function wanting 3 pieces of information: my x and y vectors and a trace. From my understanding, the trace is the index of the line that needs to be updated, but I am not sure if I did this correctly. Any ideas on how I could fix this?

Edit:

    import wx
    import random
    import numpy as np
    from wxmplot import PlotPanel

    class MainFrame(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, None, size=(1200, 900))

            self.x = np.array([0, 1, 2])
            self.y = np.array([5, 8, 4])

            self.panel_1 = Panel_one(self)
            self.panel_1.graph1.plot(self.x, self.y)
            self.button_1 = wx.Button(self, label='update graph', size=(100, 30))
            self.Bind(wx.EVT_BUTTON, self.click_button, self.button_1)
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.panel_1, 0, wx.LEFT, 5)
            sizer.Add(self.button_1, 0, wx.LEFT, 5)
            self.SetSizer(sizer)

        def click_button(self, e):
            self.x = np.append(self.x, [max(self.x)+1])
            self.y = np.append(self.y, [random.randint(0, 10)])
            self.panel_1.graph1.update_line(max(self.x), self.x, self.y)
            self.Layout()

    class Panel_one(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent, -1, style=wx.SUNKEN_BORDER)

            self.graph1 = PlotPanel(self, size=(400, 400))

    if __name__ == "__main__":
        app = wx.App(redirect=False)
        frame = MainFrame(None)
        frame.Show()
        app.MainLoop()

Solution

  • The x and y data should be numpy arrays.