使用 pyQt 4.8.5 在 Python 2.7 中编写:
如何在 pyQt 中实时更新 Matplotlib 小部件?
目前,我正在对数据进行采样(现在是 random.gauss),附加此数据并进行绘图 - 您可以看到我每次都会清除该数字并为每次调用重新绘制:
def getData(self):
self.data = random.gauss(10,0.1)
self.ValueTotal.append(self.data)
self.updateData()
def updateData(self):
self.ui.graph.axes.clear()
self.ui.graph.axes.hold(True)
self.ui.graph.axes.plot(self.ValueTotal,'r-')
self.ui.graph.axes.grid()
self.ui.graph.draw()
我的 GUI 可以工作,尽管我认为这是实现这一目标的错误方法,因为它效率非常低,我相信我应该在绘图时使用“动画调用”(?),尽管我不知道如何操作。
一个想法是在第一个绘图完成后仅更新图形对象。axes.plot
应该返回一个Line2D
您可以修改其 x 和 y 数据的对象:
http://matplotlib.org/api/artist_api.html#matplotlib.lines.Line2D.set_xdata
因此,一旦绘制了线条,不要删除并绘制新线条,而是修改现有的:
def updateData(self):
if not hasattr(self, 'line'):
# this should only be executed on the first call to updateData
self.ui.graph.axes.clear()
self.ui.graph.axes.hold(True)
self.line = self.ui.graph.axes.plot(self.ValueTotal,'r-')
self.ui.graph.axes.grid()
else:
# now we only modify the plotted line
self.line.set_xdata(np.arange(len(self.ValueTotal))
self.line.set_ydata(self.ValueTotal)
self.ui.graph.draw()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)