Постройте график живого индекса, используя matplotlib - python
Я пытаюсь построить график линий, используя matplotlib - python.
График должен выглядеть следующим образом:
я использовал patches.PathPatch
Чтобы построить график на изображении выше, с определенными вершинами и кодами, но я сталкиваюсь с некоторыми техническими проблемами, когда пытаюсь нанести живые данные выше желаемого графика. Для каждого чтения график восстанавливается (я получаю 8 цифр за 8 уникальных чтений). Как я могу предотвратить это? Мне просто нужно, чтобы изображение выше было моим фоновым графиком (как индексный график), и текущие данные должны быть нанесены над этим графиком.... Кроме того, есть ли способ упростить приведенный ниже код? Любая помощь будет принята с благодарностью
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)
1 ответ
Используя, как ваш ввод в настоящее время стилизован:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
path1 = pd.DataFrame([(0,0),(0,23),(1,18),(1,0),(0,0),(1,0),(1,18),(2,16),(2,0),(1,0),(2,0),(2,16),(3,15),(3,0),(2,0),(3,0),(3,15),(4,14),(4,0),(3,0),(4,0),(4,14),(5,13),(5,0),(4,0),(5,0),(5,13),(6,12),(6,0),(5,0),(6,0),(6,12),(7,10),(7,0),(6,0),])
path2 = pd.DataFrame([(0,23),(0,45),(1,39),(1,18),(0,23),(1,18),(1,39),(2,38),(2,16),(1,18),(2,16),(2,38),(3,34),(3,15),(2,16),(3,15),(3,34),(4,30),(4,14),(3,15),(4,14),(4,30),(5,23),(5,13),(4,14),(5,13),(5,23),(6,19),(6,12),(5,13),(6,12),(6,19),(7,16),(7,10),(6,12),])
path3 = pd.DataFrame([(0,45),(0,50),(1,50),(1,39),(0,45),(1,39),(1,50),(2,50),(2,38),(1,39),(2,38),(2,50),(3,50),(3,34),(2,38),(3,34),(3,50),(4,46),(4,30),(3,34),(4,30),(4,46),(5,32),(5,23),(4,30),(5,23),(5,32),(6,28),(6,19),(5,23),(6,19),(6,28),(7,24),(7,16),(6,19),])
path4 = pd.DataFrame([(0,50),(0,60),(1,60),(1,50),(0,50),(1,50),(1,60),(2,60),(2,50),(1,50),(2,50),(2,60),(3,60),(3,50),(2,50),(3,50),(3,60),(4,60),(4,46),(3,50),(4,46),(4,60),(5,60),(5,32),(4,46),(5,32),(5,60),(6,60),(6,28),(5,32),(6,28),(6,60),(7,60),(7,24),(6,19),])
p1y = list(path1[1][1::5])
p1y.append(path1[1][32])
p2y = list(path2[1][1::5])
p2y.append(path2[1][32])
p3y = list(path3[1][1::5])
p3y.append(path3[1][32])
p4y = list(path4[1][1::5])
p4y.append(path4[1][32])
xline = np.array(range(len(p1y)))
ax.fill_between(xline, np.zeros(len(p1y)), p1y, facecolor='green', alpha=0.8)
ax.fill_between(xline, p1y, p2y, facecolor='orange', alpha=0.8)
ax.fill_between(xline, p2y, p3y, facecolor='red', alpha=0.8)
ax.fill_between(xline, p3y, p4y, facecolor='purple', alpha=0.8)
ax.set_xlim(0,7)
ax.set_ylim(0,60)
plt.grid()
plt.show()
Однако, если вы можете упростить ваши входящие данные verts:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
path1 = [23, 18, 16, 15, 14, 13, 12, 10]
path2 = [45, 39, 38, 34, 30, 23, 19, 16]
path3 = [50, 50, 50, 50, 46, 32, 28, 24]
path4 = [60, 60, 60, 60, 60, 60, 60, 60]
xline = np.array(range(len(path1)))
ax.fill_between(xline, np.zeros(len(path1)), path1, facecolor='green', alpha=0.8)
ax.fill_between(xline, path1, path2, facecolor='orange', alpha=0.8)
ax.fill_between(xline, path2, path3, facecolor='red', alpha=0.8)
ax.fill_between(xline, path3, path4, facecolor='purple', alpha=0.8)
ax.set_xlim(0,7)
ax.set_ylim(0,60)
plt.grid()
plt.show()
Замечание о разнице: вы используете Python 2.6, и это было сделано с помощью Python 3.6.0 и Matplotlib 2.0.0