簡體   English   中英

交互式繪圖后,matplotlib沒有響應

[英]matplotlib not responding after a interactive plotting

我正在使用Arduino和python進行項目開發,正在使用庫(pyfirmata,matplot,立即繪制)從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()

我想在經過迭代后保存傳感器繪圖,這是我的最終目標

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

更一般而言,如下所示,可以更好地在事件循環中完全完成工作。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM