我试图遵循一些生成实时图的指南,例如:real-time plotting in while loop with matplotlib和{a2}
不过,我相信示例代码是用Python2.7编译的。当我试图编译我的程序时,我没有看到一个实时的图被更新。这是因为Python3不支持它吗?或者我错过了图书馆什么的?只有当我停止while循环时,才会看到最后绘制的值。我使用竞技表演作为我的IDE;这会阻止我观看实时情节吗?在
import serial
import numpy as np
import matplotlib.pyplot as plt
def plotlive():
plt.plot(ard_dat,'o')
plt.xlabel('count', fontsize=12)
plt.ylabel('reading', fontsize=12)
plt.legend(loc='upper right')
ard_dat=[]
plt.ion()
cnt=0
arduinoSerialData = serial.Serial('com5',9600)
while True:
while (arduinoSerialData.inWaiting()==0):
pass
srdata = arduinoSerialData.readline()
try:
intstrdata = int(srdata)
except ValueError:
pass
ard_dat.append(intstrdata)
drawnow(plotlive)
plt.pause(.00001)
cnt+=1
if (cnt>50):
ard_dat.pop(0)
代码中没有特定的Python2或3命令,因此您可以将其排除在等式之外。在
我不建议使用drawnow。直接调用
plotlive()
。不过,这只是一个建议,因为drawnow
是一个非常无用的包,但它不会阻止代码运行。在假设序列工作正常,那么问题中的代码在作为脚本运行时应该生成一个更新图。在
要点是:牛仔竞技表演不能制作动画。请看这个问题:https://github.com/yhat/rodeo/issues/488 原因是它使用类似笔记本的输出机制。在Jupyter笔记本中,您实际上可以将后端设置为交互模式(}),但这在竞技表演中显然是不可能的。在
%matplotlib tk
或{Rodeo似乎也没有选择在IDE之外以python脚本的形式运行一些代码。因此,我们的想法是要么使用一个不同的IDE,要么至少在竞技表演之外运行动画。在
相关问题 更多 >
编程相关推荐