我正在使用matplotlib.pyplot实时绘制一些值。我将值放在双端队列中,然后将其传递给绘图函数。我想将最大大小设置为7;因此,在插入超过第7个索引的所有内容之后,将弹出第一项,但它似乎不起作用;
python
import matplotlib.pyplot as plt
import time
from collections import deque as dq
# the auguments in this function are inter process queues which contain the data produced by another process
# the data is in a json format and looks like the following;
'''
{'memory_state': 22.7, 'host_ip': '192.168.204.130', 'net_down': 1958707880, 'time_stamp': '12:27:42', 'cpu_utilization': 0.0, 'net_up': 5998207783, 'sec': 42, 'dropped_packets': 0}
'''
def do_plot(sys_monitor_queue):
print('do_plot loop running')
list_size = 6
monitor_x_values,monitor_net_down = dq([],maxlen=list_size),dq([],maxlen=list_size)
plt.rcParams['animation.html'] ='jshtml'
fig = plt.figure(figsize=(12,8))
fig.show()
system_monitor_plot = fig.add_subplot(111)
while True:
plt.title('Network Inbound Traffic')
plt.xlabel('Time/second', fontsize=14)
plt.ylabel('Packets / mbps',fontsize=14)
plt.tick_params(axis ='both', labelsize=14)
plt.xticks(rotation = 90)
if not sys_monitor_queue.empty():
try:
rv_monitor = sys_monitor_queue.get()
monitor_net_down.append(int(rv_monitor["net_down"])/1000000)
monitor_x_values.append(rv_monitor['time_stamp'])
system_monitor_plot.plot(monitor_x_values, monitor_net_down, linewidth=1)
fig.canvas.draw()
except Exception as exception:
print("do_plot_exception :", exception)
continue
else:
time.sleep(1)
该绘图实际上是可以工作的,只是它被压缩了,因为双端队列未按预期弹出旧项目