更新wxmplot图时出错

时间:2018-04-08 21:38:27

标签: python matplotlib wxpython wxmpl

我正在尝试创建一个在Python 3.6中使用wxmplot动态更新的图形。根据这里的文档:http://cars.uchicago.edu/software/python/wxmplot/plotpanel.html#plotpanel.plot,我应该调用update_line函数,与重绘绘图相比,它允许更快的图形更新。但是,这个功能对我不起作用。这是我的代码:

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

想法是,只要单击按钮并随机生成新数据点,图形就会更新。我收到一条错误消息:AttributeError:' list'对象没有属性' min'。我不确定我做错了什么,但我认为它可能与update_line函数有关,需要3条信息:我的x和y向量和跟踪。根据我的理解,跟踪是需要更新的行的索引,但我不确定我是否正确地执行了此操作。关于如何解决这个问题的任何想法?

编辑:

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

1 个答案:

答案 0 :(得分:-1)

x和y数据应该是numpy数组。