Python matplotlib在交互式打印后没有响应

Python matplotlib在交互式打印后没有响应,python,matplotlib,drawnow,Python,Matplotlib,Drawnow,我正在使用Arduino和python进行一个项目,我正在使用库(pyfirmata、matplot、draw now)从Arduino绘制实时传感器数据,我正在获得实时输出,但在固定迭代之后,图形没有响应。我把代码附在下面 import pyfirmata import time import matplotlib.pyplot as plt from drawnow import * import sys board = pyfirmata.Arduino('COM8') iter8 = p

我正在使用Arduino和python进行一个项目,我正在使用库(pyfirmata、matplot、draw now)从Arduino绘制实时传感器数据,我正在获得实时输出,但在固定迭代之后,图形没有响应。我把代码附在下面

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()
导入pyfirmata
导入时间
将matplotlib.pyplot作为plt导入
从现在开始导入*
导入系统
board=pyfirmata.Arduino('COM8')
iter8=pyfirmata.util.Iterator(板)
iter8.start()
发光二极管=电路板。获取引脚('d:13:o')
ldr=线路板。获取引脚('a:0:o')
val=0
换算为1023
converted2=5.0/1023.0
s=[]
i=0
def makeFig():
plt.图(1)
plt.ion()
小幅图(s)
plt.title(“我的直播传感器数据”)#绘制标题
plt.grid(真)

而(i您可能希望在
plt.show()之前调用
plt.ioff()

更一般地说,最好完全在事件循环内工作,如下所示

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()

您可能想在
plt.show()之前调用
plt.ioff()

更一般地说,最好完全在事件循环内工作,如下所示

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()