matplotlib - 如何使用来自 Arduino 的串行数据更新 matplotlib 中的文本?
问题描述
我正在尝试创建一个动画图,该图使用来自我的串行端口的数据实时更新。数据由 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 的交互模式来实现。您的问题的答案与此问题的答案非常相似:基本上您需要启用交互模式,ion()
然后在不调用show()
(或相关)函数的情况下更新绘图。此外,情节和子情节只能在输入循环之前创建一次。
这是修改后的示例:
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))
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(8,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.read_until(']') #reads the value from the serial port as a string
tempsString = np.asarray(strn)
tempsFloat = np.fromstring(tempsString, dtype=float, sep= ', ')
tempsArray.flat=tempsFloat
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="w"))
# allow some delay to render the image
plt.pause(0.1)
plt.ioff()
注意:这段代码对我有用,但由于我现在没有 Arduino,我用随机生成的帧序列 ( np.random.rand(8,8,10)
) 对其进行了测试,所以我可能忽略了一些细节。让我知道它是如何工作的。
推荐阅读
- javascript - 如何使用 Javascript 关注范围滑块?
- javascript - onClick 仅在-element上触发
- java - 如何在使用 javamail 发送电子邮件时安全地使用凭据
- c# - 文件未从 File 或 PhysicalFile IActionResult 下载
- doorkeeper - 将范围限制为某些用户
- python - 在成本函数中包含与预期模型输出的偏差
- c# - 有没有办法阻止我的页面在我导航到它们时只闪烁进出?
- unix - 使用 find 命令计算某些类型的可执行文件
- mysql - 安装 MySQL 5.6 Lightsail Ubuntu
- node.js - 在 ubuntu 中的 apache 服务器 (vps) 上运行节点项目