[英]matplotlib not responding after a interactive plotting
I was working on a project using Arduino with python, I was plotting real-time sensor data from Arduino using libraries(pyfirmata,matplot,draw now) I am getting the real-time output but after the fixed iteration the figure got not responding. 我正在使用Arduino和python进行项目开发,正在使用库(pyfirmata,matplot,立即绘制)从Arduino绘制实时传感器数据,我正在获取实时输出,但是经过固定的迭代之后,该图没有响应。 I attached the code below 我附上下面的代码
import pyfirmata
import time
import matplotlib.pyplot as plt
from drawnow import *
import sys
board = pyfirmata.Arduino('COM8')
iter8 = pyfirmata.util.Iterator(board)
iter8.start()
LED = board.get_pin('d:13:o')
ldr=board.get_pin('a:0:o')
val=0
converted=1023
converted2=5.0/1023.0
s=[]
i=0
def makeFig():
plt.figure(1)
plt.ion()
plt.plot(s)
plt.title('My Live Streaming Sensor Data') # Plot the title
plt.grid(True)
while(i<=50):
time.sleep(0.01)
val=ldr.read()
print(val * converted * converted2)
s.append(val)
i=i+1
drawnow(makeFig) # Call drawnow to update our live graph
plt.pause(.000001)
plt.show()
I want to save the sensor plotting after some iteration that is my ultimate goal 我想在经过迭代后保存传感器绘图,这是我的最终目标
You probably want to call plt.ioff()
before plt.show()
. 您可能想要在plt.ioff()
之前调用plt.show()
。
More generally, better work completely inside the event loop as shown below. 更一般而言,如下所示,可以更好地在事件循环中完全完成工作。
import pyfirmata
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
board = pyfirmata.Arduino('COM8')
iter8 = pyfirmata.util.Iterator(board)
iter8.start()
LED = board.get_pin('d:13:o')
ldr=board.get_pin('a:0:o')
val=0
converted=1023
converted2=5.0/1023.0
s=[]
i=0
fig, ax = plt.subplots()
line,= ax.plot([],[])
def update(i):
val=ldr.read()
print(val * converted * converted2)
s.append(val)
line.set_data(range(len(s)), s)
ax.autoscale()
ax.relim()
ax.autoscale_view()
FuncAnimation(fig, update, frames=50, repeat=False)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.