python的plot如何实时更新,pyQt Matplotlib小部件实时数据更新

这篇博客探讨了如何在Python 2.7和PyQt4.8.5中实现实时更新Matplotlib图表的高效方法。作者当前的实现每次更新都清除并重新绘制整个图表,但认为这种方法效率低下。提出了一个解决方案,即在首次绘制后只更新图形对象,通过修改已存在的Line2D对象来改变图表数据,从而提高性能。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值