Matplotlib图更新Raspberry Pi OS后无响应

okxuctiv  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(87)

我正在使用Raspberry Pi来制作热水器的用电量和温度随时间变化的动画。数据由Arduino收集并通过NRF24L01+发送到Raspberry Pi。更新Raspberry Pi OS后,尽管控制台显示一切正常,但数字几乎立即停止响应。

import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
import datetime as dt

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from decimal import Decimal

# Create figure for plotting
fig = plt.figure()
fig.patch.set_facecolor('whitesmoke')
ax1 = fig.add_subplot(1, 1, 1)
ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

hFont = {'fontname':'sans-serif', 'weight':'bold', 'size':'22'}

xs = []
ysTemp = []
ysAC = []
ysDC = []

# This function is called periodically from FuncAnimation
def animate(i, xs, ysTemp, ysAC, ysDC):

    values = getValues()
    print(values)
    temp_c = Decimal(values[2])
    if temp_c < 0: temp_c = 0
    wAC = Decimal(values[0])
    if wAC < 0: wAC = 0
    wDC = float(values[1]) * float(values[3][:4])
    wDC = Decimal(wDC)
    if wDC < 0: wDC = 0
    
    
    # Add x and y to lists
    xs.append(dt.datetime.now().strftime('%H:%M:%S'))
    ysTemp.append(temp_c)
    ysAC.append(wAC)
    ysDC.append(wDC)

    # Limit x and y lists to 20 items
    xs = xs[-20:]
    ysTemp = ysTemp[-20:]
    ysDC = ysDC[-20:]
    ysAC = ysAC[-20:]
    
    # Draw x and y lists
    ax1.clear()   
    ax2.clear()
    
    ax1.set_ylabel('Power Consumption', **hFont)
    ax2.set_ylabel('Temperature', **hFont)
    
    lineTemp, = ax2.plot(xs, ysTemp, 'k', label='Temp')
    lineAC, = ax1.plot(xs, ysAC, 'c:', label='Mains')
    lineDC, = ax1.plot(xs, ysDC, 'y--', label='Solar')

    # Format plot
    fig.autofmt_xdate()
    plt.title('Power Consumption vs Temperature Over Time', **hFont, fontweight = 'heavy')
    plt.legend([lineAC, lineDC,lineTemp], ['Mains', 'Solar', 'Temp'])

#######################################################
GPIO.setmode(GPIO.BCM)

pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]

radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)

radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)

radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()

radio.openReadingPipe(1, pipes[1])
radio.startListening()
print("Radio Started")

#############################################################

def getValues():

    while not radio.available(0):
        time.sleep(1 / 100)
    receivedMessage = []
    radio.read(receivedMessage, radio.getDynamicPayloadSize())
  
    string = ""
    for n in receivedMessage:
        # Decode into standard unicode set
        if (n >= 32 and n <= 126):
            string += chr(n)
    measureList = list(string.split(","))

    return measureList

# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ysTemp, ysAC, ysDC), interval=1000, blit=False)
plt.get_current_fig_manager().full_screen_toggle()
plt.ioff()
plt.show()
plt.draw()

字符串

5m1hhzi4

5m1hhzi41#

结果证明这是一个简单的修复,尽管我不确定GUI是否随着Raspberry Pi OS而改变,或者它是否是matplotlib的问题。
添加以下行:

import matplotlib
matplotlib.use('TkAgg')

字符串
使用前:

import matplotlib.pyplot as plt

相关问题