添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

1586010002-jmsa.png

Writing in Python 2.7 using pyQt 4.8.5:

How may I update a Matplotlib widget in real time within pyQt?

Currently I'm sampling data (random.gauss for now), appending this and plotting - you can see that I'm clearing the figure each time and re-plotting for each call:

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

My GUI works though I think this is the wrong way to achieve this as its highly inefficient, I believe I should use the 'animate call'(?) whilst plotting, though I don't know how.

UHKxa.png

解决方案

One idea would be to update only the graphics object after the first plot was done.

axes.plot should return a Line2D object whose x and y-data you can modify:

So, once you have the line plotted, don't delete and plot a new one, but modify the existing:

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

版权声明:本文为CSDN博主「weixin_39901332」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接: https://blog.csdn.net/weixin_39901332/article/details/110032136