Seite 1 von 1

matplotlib Plot Hangs during redraw.

Verfasst: Donnerstag 27. November 2014, 11:53
von Wafeeq
Hi, I am plotting data comming from Serial port by using matplotlib plot function and actually I need to update plot with new data. Real time data updates the plot and it works fine but plot hangs if i click on the plot or I want to chang the zoom.

Code: Alles auswählen

#!/usr/bin/python

from comctrl import *
import time
import matplotlib.pyplot as plt
import numpy
from drawnow import *
import random
from datetime import datetime
from datetime import timedelta
plt.ion()
start_time = datetime.now()

# returns the elapsed milliseconds since the start of the program
def millis():
   dt = datetime.now() - start_time
   ms = (dt.days * 24 * 60 * 60 + dt.seconds) * 1000 + dt.microseconds / 1000.0
   return ms

def makeFig():
        plt.plot(tempX,tempY,'g-')
        plt.title("PFC Speed Plot")
        plt.xlabel("Time (sec)")
        plt.ylabel("Speed (rpm)")
        plt.grid(True)
        
        #plt.xlim([0,180])
        
#now = time.time()
#print(int(round(time.time() * 1000)))
samples = 0
cnt = 0
tempY = []
tempX = []
com = ComMiniVad ( port='auto' )
old_milli_time = 0

if ( com.initCommunication() == 1 ):
        com.setSpeed(5500)
        com.startPump()
        com.connectToCU()
try:
        while ( True ):
                #print (millis())
                data = com.getLatestDataPackage()
                tempY.append(data.meanSpeed)
                tempX.append(millis()/1000)
                cnt = cnt +1
                #print(data.meanSpeed," ",millis())
                samples = samples + 1
                if(cnt > 3000):
                        tempY.pop(0)
                        tempX.pop(0)
                
                if(samples > 10):
                        samples = 0
                        drawnow(makeFig)
                        

                
                        
                #print "meanSpeed: % 5u" % data.meanSpeed
                #time.sleep ( 0.1 )
except:
        pass
plt.close()
com.stopPump()
com.terminateCommunication()

Re: matplotlib Plot Hangs during redraw.

Verfasst: Donnerstag 27. November 2014, 13:48
von MagBen
You have to make sure, that the event-loop of your matplotlib backend get some CPU-time. Right now all CPU-time is consumed in your while-loop.