使用 MatPlotLib 动画的串行可视化未正确更新
Serial visualisation with MatPlotLib animate not updating properly
我想将来自压力感应垫(32x32 压力点)的值实时可视化为带有 MatPlotLib 动画的热图。
垫子输出 1025 个字节(1024 个值 + 'end byte',始终为 255)。我从 animate
函数中打印出来,但只有在我注释掉 plt.imshow(np_ints)
.
时它才有效
使用 plt.imshow
时,MatPlotLib window 会弹出,甚至会读取值...当我按下传感器启动程序时,我在热图中看到了它,但当我松开它时, 它似乎慢慢地遍历了串行缓冲区中的所有读数,而不是实时的。不确定是因为我没有正确处理序列还是与 FuncAnimation 的工作方式有关。有人能给我指出正确的方向吗?
import numpy as np
import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
np.set_printoptions(threshold=1024,linewidth=1500)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
def animate(i):
# np_ints = np.random.random((200, 200)) # FOR TESTING ONLY
if ser.inWaiting:
ser_bytes = bytearray(ser.read_until(b'\xFF')) # should read 1025 bytes (1024 values + end byte)
if len(ser_bytes) != 1025: return # prevent error from an 'incomplete' serial reading
ser_ints = [int(x) for x in ser_bytes]
np_ints = np.array(ser_ints[:-1]) # drop the end byte
np_ints = np_ints.reshape(32, 32)
print(len(ser_ints))
print(np.matrix(np_ints))
plt.imshow(np_ints) # THIS BRAKES IT
if __name__ == '__main__':
ser = serial.Serial('/dev/tty.usbmodem14101', 11520)
ser.flushInput()
ani = animation.FuncAnimation(fig, animate, interval=10)
plt.show()
下面的代码允许使用 blitting
对随机数进行动画处理。诀窍是不使用 plt.imshow
而是更新艺术家数据。 plt.imshow
将通过获取当前轴创建另一个图像。放缓将是由当时图中的许多艺术家造成的。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
np.set_printoptions(threshold=1024,linewidth=1500)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
# create dummy data
h = ax.imshow(np.random.rand(32, 32))
def animate(i):
# np_ints = np.random.random((200, 200)) # FOR TESTING ONLY
# put here code for reading data
np_ints = np.random.rand(32, 32) # not ints here, but principle stays the same
# start blitting
h.set_data(np_ints)
return h
if __name__ == '__main__':
ani = animation.FuncAnimation(fig, animate, interval=10)
plt.show()
我想将来自压力感应垫(32x32 压力点)的值实时可视化为带有 MatPlotLib 动画的热图。
垫子输出 1025 个字节(1024 个值 + 'end byte',始终为 255)。我从 animate
函数中打印出来,但只有在我注释掉 plt.imshow(np_ints)
.
使用 plt.imshow
时,MatPlotLib window 会弹出,甚至会读取值...当我按下传感器启动程序时,我在热图中看到了它,但当我松开它时, 它似乎慢慢地遍历了串行缓冲区中的所有读数,而不是实时的。不确定是因为我没有正确处理序列还是与 FuncAnimation 的工作方式有关。有人能给我指出正确的方向吗?
import numpy as np
import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
np.set_printoptions(threshold=1024,linewidth=1500)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
def animate(i):
# np_ints = np.random.random((200, 200)) # FOR TESTING ONLY
if ser.inWaiting:
ser_bytes = bytearray(ser.read_until(b'\xFF')) # should read 1025 bytes (1024 values + end byte)
if len(ser_bytes) != 1025: return # prevent error from an 'incomplete' serial reading
ser_ints = [int(x) for x in ser_bytes]
np_ints = np.array(ser_ints[:-1]) # drop the end byte
np_ints = np_ints.reshape(32, 32)
print(len(ser_ints))
print(np.matrix(np_ints))
plt.imshow(np_ints) # THIS BRAKES IT
if __name__ == '__main__':
ser = serial.Serial('/dev/tty.usbmodem14101', 11520)
ser.flushInput()
ani = animation.FuncAnimation(fig, animate, interval=10)
plt.show()
下面的代码允许使用 blitting
对随机数进行动画处理。诀窍是不使用 plt.imshow
而是更新艺术家数据。 plt.imshow
将通过获取当前轴创建另一个图像。放缓将是由当时图中的许多艺术家造成的。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
np.set_printoptions(threshold=1024,linewidth=1500)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
# create dummy data
h = ax.imshow(np.random.rand(32, 32))
def animate(i):
# np_ints = np.random.random((200, 200)) # FOR TESTING ONLY
# put here code for reading data
np_ints = np.random.rand(32, 32) # not ints here, but principle stays the same
# start blitting
h.set_data(np_ints)
return h
if __name__ == '__main__':
ani = animation.FuncAnimation(fig, animate, interval=10)
plt.show()