我正在使用pyserial从arduino串行监视器接收32个浮点数到python的数组。此外,我想用从Arduino串行监视器接收到的数字(温度值)绘制带注释的热图。我能够在python shell和热图中接收温度值。但是我面临的挑战是我认为这些值不是同步的,即我应该怎么做才能映射具有特定值的单元格。
我附上以下代码:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import serial
import time
tempdata = serial.Serial("COM8",38400,timeout=0.1)
strn = []
rows = ["A","B","C","D"]
columns = ["1", "2", "3", "4",
"5", "6", "7","8"]
tempsArray = np.empty((1,32))
plt.ion()
fig, ax = plt.subplots()
# The subplot colors do not change after the first time
# if initialized with an empty matrix
im = ax.imshow(np.random.rand(4,8),cmap='plasma')
# Axes ticks
ax.set_xticks(np.arange(len(columns)))
ax.set_yticks(np.arange(len(rows)))
# Axes labels
ax.set_xticklabels(columns)
ax.set_yticklabels(rows)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
ax.set_title("")
fig.tight_layout()
text = []
while True: #Makes a continuous loop to read values from Arduino
tempdata.flush()
strn = tempdata.readline()[:] #reads the value from the serial port as a
string
tempsString = np.asarray(strn)
tempsFloat = np.fromstring(tempsString, dtype=float, sep= ',')
tempsArray.flat=tempsFloat
print(tempsArray)
im.set_array(tempsArray)
#Delete previous annotations
for ann in text:
ann.remove()
text = []
#Loop over data dimensions and create text annotations.
for i in range(len(rows)):
for j in range(len(columns)):
text.append(ax.text(j, i, tempsArray[i, j],
ha="center", va="center", color="b"))
# allow some delay to render the image
plt.pause(0.1)
plt.ioff()