【发布时间】:2018-07-16 00:52:07
【问题描述】:
我正在尝试创建一个动画图,该图使用来自我的串行端口的数据实时更新。数据由 8x8 阵列中的 Arduino 流入。数据是红外热像仪的温度。我能够创建一个图形的实例,但我无法使用串行流数据更新文本。
我尝试设置 'plt.show(block=False)' 以便脚本继续执行,但这会使该图完全为空,并将其缩放为一个带有加载光标的小窗口,该加载光标将继续加载。
我只希望文本使用数组数据以及新规范化数据中的颜色进行更新。
如何使用 matplotlib 中的串行数据更新文本?
谢谢!
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import serial
import time
tempdata = serial.Serial("COM3",9600)
tempdata.timeout = 100
strn = []
rows = ["A", "B", "C", "D",
"E", "F", "G","H"]
columns = ["1", "2", "3", "4",
"5", "6", "7","8"]
print("AMG8833 8x8 Infrared Camera")
time.sleep(0.75)
print("Connected to: " + tempdata.portstr)
time.sleep(0.75)
print("Initializing Camera...")
tempsArray = np.empty((8,8))
while True: #Makes a continuous loop to read values from Arduino
fig, ax = plt.subplots()
im = ax.imshow(tempsArray,cmap='plasma')
tempdata.flush()
strn = tempdata.read_until(']') #reads the value from the serial port as a string
tempsString = np.asarray(strn)
tempsFloat = np.fromstring(tempsString, dtype=float, sep= ', ')
# 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")
tempsArray.flat=tempsFloat
im.set_array(tempsArray)
ax.set_title("")
fig.tight_layout()
#Loop over data dimensions and create text annotations.
for i in range(len(rows)):
for j in range(len(columns)):
text = ax.text(j, i, tempsArray[i, j],
ha="center", va="center", color="w")
plt.show()
【问题讨论】:
标签: matplotlib arduino pyserial colormap