Python 使用Matplotlib中的极坐标直方图显示实时MQTT数据

Python 使用Matplotlib中的极坐标直方图显示实时MQTT数据,python,matplotlib,mqtt,Python,Matplotlib,Mqtt,我有一个MQTT客户机以每秒近10条消息的频率接收数据。 数据包含来自不同方向的声源信息。 我使用Matplotlib在极坐标直方图上显示了这些信息 我使用了plt.pause(time)函数使图形动画化。但使用这种方法会有很大的延迟,极坐标图的更新速度与我收到的数据相比非常缓慢 有没有什么方法可以加快这个过程,或者有没有更好的方法可以使用?我曾尝试将时间值从1e-60更改为1e-3,但似乎没有任何区别 任何帮助都将不胜感激 (注: 我曾尝试使用matplotlib.animation(),但问

我有一个MQTT客户机以每秒近10条消息的频率接收数据。 数据包含来自不同方向的声源信息。 我使用Matplotlib在极坐标直方图上显示了这些信息

我使用了plt.pause(time)函数使图形动画化。但使用这种方法会有很大的延迟,极坐标图的更新速度与我收到的数据相比非常缓慢

有没有什么方法可以加快这个过程,或者有没有更好的方法可以使用?我曾尝试将时间值从1e-60更改为1e-3,但似乎没有任何区别

任何帮助都将不胜感激

(注: 我曾尝试使用matplotlib.animation(),但问题是,有两个循环应该同时执行(一个用于mqtt消息,另一个用于matplotlib动画)。因此,在这种情况下,一次只执行一个循环。此处将解释此问题。)

import os, sys, json, datetime
import paho.mqtt.client as mqtt
import numpy as np
import matplotlib.pyplot as plt
import math

ax = plt.subplot(111, projection='polar')
plt.title('Data from Sound Source')

theta = [0]*4
radii = [0]*4
bars = ax.bar(theta, radii, width=0.05, bottom=0.0)

#MQTT function for receiving messages periodically 

def on_message(client, userdata, msg):
    global theta, radii
    ax.clear()
    theta = [0]*4
    radii = [0]*4

    sources = json.loads(msg.payload)            
    length = len(sources)

    for i in range(length):
        x = (sources[i]['x'][0])
        y = (sources[i]['x'][1])
        theta[i] =   math.atan2(y,x)
        radii[i] =   math.sqrt(x**2 + y**2)
    bars = ax.bar(theta, radii, width=0.1, bottom=0.0, tick_label=None)
    plt.pause(1e-2) #to make graph wait to update the data

# MQTT variables
host = "localhost"
port = 1883
topic = "hark/data/sentenceinfo"
        
client = mqtt.Client("HRSUB") #create new instance
client.connect(host, port) #connect to broker
print("Subscribing to topic", topic)
client.subscribe(topic)
client.on_message = on_message
client.loop_forever()