2015-03-03 91 views
1

我试图在matplotlib来创建这样一个计: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

这里有很多代码。我很多人愿意帮助你理解正在发生的事情,但不愿意通过那么多(可能是不相关的)代码。大多数与mpl相关的问题都可以用<20行代码进行提问。如果SO给你一个滚动条,你粘贴了太多的代码。 – tacaswell 2015-03-04 03:11:30

+0

我已经自己解决了。无论如何感谢您的建议。然而,缩短代码并不容易,因为有两个库(Pyqt4和matplotlib)必须一起工作。因此,为了明确这个问题,有必要确定代码有点长。我试图通过在代码中加入更多评论来使其可读。 – Driedan 2015-03-05 11:09:33

+0

请回答你自己的问题。 – tacaswell 2015-03-05 14:03:05

回答

0

我自己解决了。在上面的代码中,程序本身会生成一个介于0-10之间的数字(在update_figure方法中)。 Supose的数字是四。

这意味着,在楔:

theta1 = 180-number/10*179 
    #as always theta2: 
    theta2 =180 

5秒后的程序产生另一号码(例如:6.5)

这意味着楔形的属性必须改变。

通过增加对象中的计的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-loop动态和缓慢,并让楔子以100 ms的休眠时间吸引自己10倍(withta1从旧号码改变为新号码10步)。如果这个解释不清楚,问我,我会试着再解释一遍。

+0

你应该不需要每次都通过循环添加楔子 – tacaswell 2015-03-10 14:30:26

+0

@tcaswell对不起,我迟到了,但是我找不到每次都不添加楔子的解决方案(它减慢了程序的速度)我想知道是否你知道一个解决方案? – Driedan 2015-04-13 13:17:39

+0

您只需要添加一次。 – tacaswell 2015-04-13 17:10:13