2013-07-08 34 views
0

我有一些简单的python代码,可以为每秒查看数据而生成实时监视器。它使用matplotlib,工作得很好,除了有内存泄漏。脚本的内存使用情况在一天内缓慢上升,似乎没有限制。我承认编程python是新手,所以想知道是否有人能看到我正在做的事情,显然是很糟糕的。预先感谢您的帮助。Python实时绘图内存泄漏

import time 
import numpy as np 
import matplotlib 
from matplotlib import figure 
import matplotlib.pyplot as plt 
import pylab as p 
import os 
import subprocess as sp 
from subprocess import Popen, PIPE 

def main(): 
    #####Initialize the plot##### 
    fig = plt.figure() 
    ax1 = fig.add_subplot(1,1,1,axisbg='black') #Set up basic plot attributes 
    ax1.set_title('Blip Current vs. Time',color='blue')  
    ax1.set_xlabel('Time (hrs)',color='blue') 
    ax1.set_ylabel('Blip Current',color='blue') 
    for t in ax1.xaxis.get_ticklines(): t.set_color('yellow') 
    for t in ax1.xaxis.get_ticklabels(): t.set_color('yellow') 
    for t in ax1.yaxis.get_ticklines(): t.set_color('white') 
    for t in ax1.yaxis.get_ticklabels(): t.set_color('purple') 
    plt.ion() #Set interactive mode 
    plt.show(False) #Set to false so that the code doesn't stop here 
    i=0 #initialize counter variable (this will help me to limit the number of points displayed on graph 

    ###Update the plot continuously### 
    while True: #This is a cheap trick to keep updating the plot, i.e. create a real time data monitor 
     blip=Popen('adoIf -vo -6 lxf.blip_b3 dataBarM', shell=True, stdout=PIPE).communicate()[0] #Get data to plot 
     hr=float(time.strftime('%H')) 
     mins=time.strftime('%M') 
     secs=time.strftime('%S') 
     secadj=float(secs)/3600 
     minadj=float(mins)/60 
     currenttime=float(hr+minadj+secadj) #Put time into format for easier plotting, i.e. 21.50 for 9:30 pm 
     if currenttime >= 0 and currenttime < 0.22: #Set x range properly when rolling over to midnight 
      xmin=0 
      xmax=currenttime+.01 
     else: 
      xmin=currenttime-.22 #Limit data to be displayed, only care about recent past 
      xmax=currenttime+.01 
     try: 
      blip =float(blip) #This throws an error if for some reason the data wasn't received at the top of the while statement 
     except ValueError: 
      blip=0.0 
     if i>300: #Limit displayed points to save memory (hopefully...) 
      del ax1.lines[0] #After 300 points, start deleting the first point each time 
     else: 
      i +=1 
     if blip > 6: #Plot green points if current is above threshold 
      ax1.plot(currenttime,blip,marker='o', linestyle='--',c='g') 
     else: #Plot red points if current has fallen off 
      ax1.plot(currenttime,blip,marker='o', linestyle='--',c='r') 
     plt.axis([xmin,xmax,None,None]) #Set xmin/xmax to limit displayed data to a reasonable window 
     plt.draw() 
     time.sleep(2) #Update every 2 seconds 

if __name__=='__main__': 
    print 'Starting Monitor' 
    main() 
+1

你为什么要使用一个字符串(使用plt.ion()不推荐用于复杂的脚本。)''的true''而不是布尔TRUE;? – user2357112

+0

因为我是新手。谢谢,我改变了它。但我怀疑这与我的记忆问题无关。 – crownjbl

+0

你在Linux,Windows,Mac上运行吗?子进程处理对每个处理都有点不同。 – ixe013

回答

1

我敢肯定,你需要每次都清除图形,否则matplotlib将继续创建一大堆新对象,并且事物不会被垃圾收集。尝试是这样的:

fig.clf() 

为while循环中的第一件事情。

+0

谢谢,但我认为这不会起作用,因为要保持实时图显示过去〜20分钟的数据可见。如果我添加,那么我只是得到一个空白图。 – crownjbl

+0

我试图在使用del ax1.lines [0]绘制300点后删除点,该点用于摆脱点(即从图中消失),但不知何故数据似乎仍然堆积如山。 – crownjbl

+0

那么如果你不清除这个图形,那么matplotlib每次你做一个ax.plot()就会创建一个新的对象。随着过程的继续,这将导致内存使用量的增加。 – reptilicus

1

尤里卡!我想通了(至少,一个解决方法)。我从while循环中取出了ax1.plot命令,并使用'set_xdata'和'set_ydata'命令以及fig.canvas.draw()命令。感谢大家的帮助,尤其是reptilicus,指出ax.plot命令每次调用它时都会创建一个新对象。

要绘制的x和y值现在存储在数组中,每个数组中的第一个元素在while循环的每次迭代中都被删除(在绘制了特定数量的点之后,其数目在该代码使用简单的索引号i)。内存使用情况持平,CPU使用率较低。代码如下:

def main(): 
    #####Initialize the plot attributes##### 
    fig = plt.figure() 
    ax1 = fig.add_subplot(1,1,1, axisbg='black')#Set up basic plot attributes 
    ax1.set_title('Blip Current vs. Time',color='blue')  
    ax1.set_xlabel('Time (hrs)',color='blue') 
    ax1.set_ylabel('Blip Current',color='blue') 
    for t in ax1.xaxis.get_ticklines(): t.set_color('yellow') 
    for t in ax1.xaxis.get_ticklabels(): t.set_color('yellow') 
    for t in ax1.yaxis.get_ticklines(): t.set_color('white') 
    for t in ax1.yaxis.get_ticklabels(): t.set_color('purple') 
    plt.ion() #Set interactive mode 
    plt.show(False) #Set to false so that the code doesn't stop here 
    i=0 #initialize counter variable (this will help me to limit the number of points displayed on graph 

    ###Initialize x values#### 
    times=[] #Create blank array to hold x values 
    hr=float(time.strftime('%H')) #Hours 
    mins=time.strftime('%M') #Minutes 
    secs=time.strftime('%S') #Seconds 
    secadj=float(secs)/3600 
    minadj=float(mins)/60 
    currenttime=float(hr+minadj+secadj) #Put time into format for easier plotting, i.e. 21.50 for 9:30 pm 
    times.append(currenttime) #Add first x value to x value array 
    if currenttime >= 0 and currenttime < 0.22: #Set x range properly when rolling over to midnight 
     xmin=0 
     xmax=currenttime+.01 
    else: 
     xmin=currenttime-.22 #Limit data to be displayed, only care about recent past 
     xmax=currenttime+.01 

    ###Initialize y values### 
    blipcur=[] #Create blank array to hold y values 
    blip=Popen('adoIf -vo -6 lxf.blip_b3 dataBarM', shell=True, stdout=PIPE).communicate()[0] #Get first datapoint for plot 
    try: 
     blip =float(blip) #This throws an error if for some reason the data wasn't received at the top of the while statement 
    except ValueError: 
     blip=0.0 
    blipcur.append(blip) #Add first y value to y value array 

    ###Initialize plot### 
    line1, = ax1.plot(times, blipcur, 'g-', marker='o') 

    ###Update the plot continuously### 
    while True: #This is a cheap trick to keep updating the plot, i.e. create a real time data monitor 
     hr=float(time.strftime('%H')) #Get new x data for plotting (get current time) 
     mins=time.strftime('%M') 
     secs=time.strftime('%S') 
     secadj=float(secs)/3600 
     minadj=float(mins)/60 
     currenttime=float(hr+minadj+secadj) #Put time into format for easier plotting, i.e. 21.50 for 9:30 pm 
     times.append(currenttime) #Add latest point to x value array 
     if currenttime >= 0 and currenttime < 0.22: #Set x range properly when rolling over to midnight 
      xmin=0 
      xmax=currenttime+.01 
     else: 
      xmin=currenttime-.22 #Limit data to be displayed, only care about recent past 
      xmax=currenttime+.01 

     blip=Popen('adoIf -vo -6 lxf.blip_b3 dataBarM', shell=True, stdout=PIPE).communicate()[0] #Get new y data for plotting 
     try: 
      blip =float(blip) #This throws an error if for some reason the data wasn't received from previous line of code 
     except ValueError: #Just set to zero if there was some temporary error 
      blip=0.0 
     blipcur.append(blip) #Add latest point to y value array 

     if i>285: #Limit data points shown on graph. Saves memory. 
      del blipcur[0] #Delete first element in y value array (oldest/first plotted point) 
      del times[0] #Delete first element in x value array 
     else: 
      i +=1 #Only want to keep making number bigger until I get over the threshold for # points I want, then why bother 

     line1.set_xdata(times) #Set x data for plot from x value array 
     plt.axis([xmin,xmax,-2,50]) #Set xmin/xmax to limit displayed data to a reasonable window 
     line1.set_ydata(blipcur) #Set y data for plot from y value array 
     fig.canvas.draw() #Update plot 
     time.sleep(2.6) #Update every 2.6 seconds 

if __name__=='__main__': 
    print 'Starting Monitor' 
    main() 
0

你原来的代码表明您想取决于blip值不同颜色的圆点。使用您的新解决方案,使用set_data,您需要为每种颜色提供新的Line2D。另一种方法是使用散点图而不是线图。散点图可以为图中的每个点分配不同的颜色。


如果你想有一个固定大小的名单,说285元,而不是 这样做:

if i>285: #Limit data points shown on graph. Saves memory. 
     del blipcur[0] #Delete first element in y value array (oldest/first plotted point) 
     del times[0] #Delete first element in x value array 

你可以使用collection.dequesmaxlen=285deques,你可以追加到你的心脏的喜悦和deque将放弃最旧的元素,当它太满。这会让你的代码更简单一些,因为你不必自己管理它的大小。 deques也可以在弹出的第一个元素在关O(1)时间,而在lists O(n)的时间弹出的第一个元素了,所以有theoretical performance增益也在这里,虽然只有约300个元素是不会做出有显着差异。


如果你有Matplotlib 1.2版或更新版本,您可以使用它FuncAnimation类。这将为您处理大部分样板代码。此外,它可以让你避免调用plt.ion()


import numpy as np 
import matplotlib.pyplot as plt 
import collections 
import datetime as DT 
import matplotlib.animation as animation 


def currenttime(): 
    now = DT.datetime.now() 
    hr = now.hour 
    mins = now.minute/60 
    secs = now.second/3600 
    return float(hr + mins + secs) 


def main(): 
    def animate(data, pathcol): 
     xvals, yvals, colors = data 
     assert len(xvals)<=300 
     if len(xvals) > 1: 
      ax.set_xlim(xvals.min(), xvals.max()) 
     pathcol.set_array(colors) 
     pathcol.set_offsets(np.column_stack([xvals, yvals])) 

    def step(): 
     xvals = collections.deque([], maxlen=N) 
     yvals = collections.deque([], maxlen=N) 
     colors = collections.deque([], maxlen=N) 
     fudge = 0 
     while True: 
      blip = np.random.random() * 10 
      xvals.append(currenttime() + fudge) 
      yvals.append(blip) 
      colors.append(1 if blip > 6 else 0) 
      yield np.asarray(xvals), np.asarray(yvals), np.asarray(colors) 
      # make time go faster 
      fudge += 0.0001 

    # Initialize the plot##### 
    N = 300 
    fig = plt.figure() 
    ax = fig.add_subplot(
     1, 1, 1, axisbg='black') # Set up basic plot attributes 
    ax.set_title('Blip Current vs. Time', color='blue') 
    ax.set_xlabel('Time (hrs)', color='blue') 
    ax.set_ylabel('Blip Current', color='blue') 

    for t in ax.xaxis.get_ticklines(): 
     t.set_color('yellow') 
    for t in ax.xaxis.get_ticklabels(): 
     t.set_color('yellow') 
    for t in ax.yaxis.get_ticklines(): 
     t.set_color('white') 
    for t in ax.yaxis.get_ticklabels(): 
     t.set_color('purple') 
    pathcol = ax.scatter([0,24], [0,10], 
          c=[0,1], s=100, 
          cmap=plt.get_cmap('RdYlGn'), vmin=0, vmax=1) 
    ani = animation.FuncAnimation(
     fig, animate, step, interval=20, fargs=(pathcol,)) 
    plt.show() 


if __name__ == '__main__': 
    print 'Starting Monitor' 
    main() 
+0

谢谢!很多好的建议。不幸的是,我无法访问我们工作系统上的动画。但愿我做了,可惜我没有;你给我的例子代码绝对是更清洁。我将结合您建议的其他更改,感谢提示。在我的编程方法中,我承认有点野蛮(不是很有经验),并且感谢有人向我展示了一种更好的方法来完成某件事。 – crownjbl