我正在尝试创建一个使用 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' object has no attribute 'min'。我不确定我做错了什么,但我认为这可能与需要 3 条信息的 update_line 函数有关:我的 x 和 y 向量以及跟踪。根据我的理解,trace 是需要更新的行的索引,但我不确定我是否正确执行此操作。关于如何解决这个问题的任何想法?
编辑:
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()
x 和 y 数据应该是 numpy 数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句