I'm trying to plot a lines graph using matplotlib - python.
The graph should look like below image:
I used patches.PathPatch
to graph the image above, with specific vertices and codes, but I'm facing some technical issues when trying to plot live data above what the desired graph. For each reading, the graph gets regenerated (I get total of 8 figures for 8 unique readings). How do I stop this from happening? I just need the image above to be my background graph (as index graph), and live data to be plotted above that graph.... Also, any way to simplify below code? Any help would be greatly appreciated
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
import serial # import Serial Library
import numpy # Import numpy
from drawnow import *
tempF= []
pressure=[]
arduinoData = serial.Serial('/dev/cu.AdafruitEZ-Link64b9-SPP', 9600) #Creating our serial object named arduinoData
plt.ion() #Tell matplotlib you want interactive mode to plot live data
cnt=0
fig = plt.figure()
ax = fig.add_subplot(111)
def makeFig(): #Create a function that makes our desired plot
codes = [Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,
Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,
Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,
Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,
Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,
Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,
Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,
]
verts1 = [
(0, 0), # left, bottom
(0, 23), # left, top
(1, 18), # right, top
(1, 0), # right, bottom
(0, 0), # ignored
(1, 0), # left, bottom
(1, 18), # left, top
(2, 16), # right, top
(2, 0), # right, bottom
(1, 0), # ignored
(2, 0), # left, bottom
(2, 16), # left, top
(3, 15), # right, top
(3, 0), # right, bottom
(2, 0), # ignored
(3, 0), # left, bottom
(3, 15), # left, top
(4, 14), # right, top
(4, 0), # right, bottom
(3, 0), # ignored
(4, 0), # left, bottom
(4, 14), # left, top
(5, 13), # right, top
(5, 0), # right, bottom
(4, 0), # ignored
(5, 0), # left, bottom
(5, 13), # left, top
(6, 12), # right, top
(6, 0), # right, bottom
(5, 0), # ignored
(6, 0), # left, bottom
(6, 12), # left, top
(7, 10), # right, top
(7, 0), # right, bottom
(6, 0), # ignored
]
verts2 = [
(0, 23), # left, bottom
(0, 45), # left, top
(1, 39), # right, top
(1, 18), # right, bottom
(0, 23), # ignored
(1, 18), # left, bottom
(1, 39), # left, top
(2, 38), # right, top
(2, 16), # right, bottom
(1, 18), # ignored
(2, 16), # left, bottom
(2, 38), # left, top
(3, 34), # right, top
(3, 15), # right, bottom
(2, 16), # ignored
(3, 15), # left, bottom
(3, 34), # left, top
(4, 30), # right, top
(4, 14), # right, bottom
(3, 15), # ignored
(4, 14), # left, bottom
(4, 30), # left, top
(5, 23), # right, top
(5, 13), # right, bottom
(4, 14), # ignored
(5, 13), # left, bottom
(5, 23), # left, top
(6, 19), # right, top
(6, 12), # right, bottom
(5, 13), # ignored
(6, 12), # left, bottom
(6, 19), # left, top
(7, 16), # right, top
(7, 10), # right, bottom
(6, 12), # ignored
]
verts3 = [
(0, 45), # left, bottom
(0, 50), # left, top
(1, 50), # right, top
(1, 39), # right, bottom
(0, 45), # ignored
(1, 39), # left, bottom
(1, 50), # left, top
(2, 50), # right, top
(2, 38), # right, bottom
(1, 39), # ignored
(2, 38), # left, bottom
(2, 50), # left, top
(3, 50), # right, top
(3, 34), # right, bottom
(2, 38), # ignored
(3, 34), # left, bottom
(3, 50), # left, top
(4, 46), # right, top
(4, 30), # right, bottom
(3, 34), # ignored
(4, 30), # left, bottom
(4, 46), # left, top
(5, 32), # right, top
(5, 23), # right, bottom
(4, 30), # ignored
(5, 23), # left, bottom
(5, 32), # left, top
(6, 28), # right, top
(6, 19), # right, bottom
(5, 23), # ignored
(6, 19), # left, bottom
(6, 28), # left, top
(7, 24), # right, top
(7, 16), # right, bottom
(6, 19), # ignored
]
verts4 = [
(0, 50), # left, bottom
(0, 60), # left, top
(1, 60), # right, top
(1, 50), # right, bottom
(0, 50), # ignored
(1, 50), # left, bottom
(1, 60), # left, top
(2, 60), # right, top
(2, 50), # right, bottom
(1, 50), # ignored
(2, 50), # left, bottom
(2, 60), # left, top
(3, 60), # right, top
(3, 50), # right, bottom
(2, 50), # ignored
(3, 50), # left, bottom
(3, 60), # left, top
(4, 60), # right, top
(4, 46), # right, bottom
(3, 50), # ignored
(4, 46), # left, bottom
(4, 60), # left, top
(5, 60), # right, top
(5, 32), # right, bottom
(4, 46), # ignored
(5, 32), # left, bottom
(5, 60), # left, top
(6, 60), # right, top
(6, 28), # right, bottom
(5, 32), # ignored
(6, 28), # left, bottom
(6, 60), # left, top
(7, 60), # right, top
(7, 24), # right, bottom
(6, 19), # ignored
]
path = Path(verts1, codes)
path2 = Path(verts2, codes)
path3 = Path(verts3, codes)
path4 = Path(verts4, codes)
patch = patches.PathPatch(path, facecolor='green', alpha=0.8)
patch2 = patches.PathPatch(path2, facecolor='orange', alpha=0.8)
patch3 = patches.PathPatch(path3, facecolor='red', alpha=0.8)
patch4 = patches.PathPatch(path4, facecolor='purple', alpha=0.8)
#ax.axhline(5, linestyle='--', color='k') # horizontal lines
#ax.axvline(0, linestyle='--', color='k') # vertical lines
ax.add_patch(patch)
ax.add_patch(patch2)
ax.add_patch(patch3)
ax.add_patch(patch4)
ax.set_xlim(0,7)
ax.set_ylim(0,60)
plt.grid()
plt.show()
plt.plot(tempF, 'ro-', label='sensor 1') #plot the temperature
plt.legend(loc='upper left') #plot the legend
plt.plot(pressure, 'bo-', label='sensor 2') #plot the temperature
plt.legend(loc='upper left')
while True: # While loop that loops forever
while (arduinoData.inWaiting()==0): #Wait here until there is data
pass #do nothing
arduinoString = arduinoData.readline() #read the line of text from the serial port
dataArray = arduinoString.split(',') #Split it into an array called dataArray
print dataArray[0]
print dataArray[1]
temp = float( dataArray[0]) #Convert first element to floating number and put in temp
P = float( dataArray[1]) #Convert second element to floating number and put in P
tempF.append(temp) #Build our tempF array by appending temp readings
pressure.append(P) #Building our pressure array by appending P readings
drawnow(makeFig) #Call drawnow to update our live graph
plt.pause(.000001) #Pause Briefly. Important to keep drawnow from crashing
cnt=cnt+1
if(cnt>150): #If you have 50 or more points, delete the first one from the array
tempF.pop(0) #This allows us to just see the last 50 data points
pressure.pop(0)