[英]matplotlib gauge update dynamically
我试图在matplotlib中创建这样的仪表:
http://www.highcharts.com/demo/gauge-solid
它几乎可以工作。 到目前为止,这是代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.patches import Wedge
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import sys,os
from PyQt4 import QtGui,QtCore
class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None):
self.fig = Figure(facecolor='#DBE0E4')
self.axes = self.fig.add_subplot(111,axisbg='#DBE0E4')
self.axes.axis('off')
#here I create the contour of the gauge
#the two horizontal lines
x1=[-1,-0.6]
x2=[0.6,1.0]
y=[0.004,0.004]
self.axes.plot(x1,y,color='#646464')
self.axes.plot(x2,y,color='#646464')
#and the 2 circles(inner and outer)
circle1=plt.Circle((0,0),radius=1,fill=False)
circle1.set_edgecolor('#646464')
circle1.set_facecolor(None)
circle2=plt.Circle((0,0),radius=0.6,fill=False)
circle2.set_edgecolor('#646464')
circle2.set_facecolor(None)
self.axes.add_patch(circle1)
self.axes.add_patch(circle2)
#Scaling of the figure
self.axes.axis('scaled')
self.axes.set_xlim(-1.1,1.1)
self.axes.set_ylim(0,1.1)
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
FigureCanvas.updateGeometry(self)
class Gauge(MyMplCanvas):
def __init__(self,meter,parent=None):
MyMplCanvas.__init__(self)
self.patches=[]
#here I create the wedge to start
self.wedge=Wedge((0,0),0.99,1,179,width=0.38,facecolor='#FF0000')
self.patches.append(self.wedge)
self.timeClear=0
self.update_figure()
#self.Online_meter=meter
#here starts the update
timer=QtCore.QTimer(self)
timer.timeout.connect(self.update_figure)
timer.start(5000)
def update_figure(self):
#here is the update command
#every 5 sec, I call value from a measurement instrument
#here I let the program generate random values
self.timeClear=self.timeClear+1
#new_data=self.Online_meter.__call__()
self.wedge.set_theta1(180-np.random.random(1)*10/10*179)
self.axes.add_patch(self.wedge)
self.draw()
到现在为止,这是可行的。 编写代码的目的是将其添加为PyQt 4程序中的小部件。 他们只剩下一个问题:当值更新时,量规会突然改变,但我希望看到量规会更新(所以我想看到楔形的角度发生变化,所以移动缓慢),希望你们能帮我。 也许他们已经是在pyqt4 GUI中插入美观仪表的好库了吗?
感谢您提前帮助我!
我自己解决了。 在上面的代码中,程序本身生成一个介于0到10之间的数字(在update_figure方法中)。 假设数字是四。
这意味着在楔子上:
theta1 = 180-number/10*179
#as always theta2:
theta2 =180
5秒钟后,程序将生成另一个数字(例如:6.5)
这意味着楔形的属性必须改变。
通过在对象Gauge的update_figure方法中添加for循环:
def update_figure(self):
self.timeClear=self.timeClear+1
#the old value becomes temp
temp=self.new_data
self.number=np.random.random(1)*10
self.new_data=180-self.number/10*179
step=(self.new_data-temp)/10
for x in range(10):
self.wedge.set_theta1(temp+step*x)
self.axes.add_patch(self.wedge)
time.sleep(1/10)
self.draw()
通过取新旧数字之间的差并将其除以10,可以通过添加此for循环并让楔形图自身绘制1O次来轻松地动态且动态地更改量规(其中theta1从从旧号码到新号码)的睡眠时间为100毫秒。 如果这种解释不清楚,请问我,我将尝试再次解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.