python cpu监视器绘图需要太多cpu才能使用matplotlib进行绘图

时间:2018-09-07 17:21:25

标签: python matplotlib cpu-usage psutil

监视所有CPU都很好,但是在绘制所有CPU的1个CPU时,Matplotlib为此加载了75%的动画效果。

我试图改善绘图

linelist[v].set_ydata(ylist[v])
fig.canvas.draw()
fig.canvas.flush_events()

如以下说明:https://bastibe.de/2013-05-30-speeding-up-matplotlib.html

完整代码:

import psutil
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
from pylab import *
import numpy as np

fig = plt.figure()
cpu_total = psutil.cpu_count()
subplots_adjust(hspace=0.000)
number_of_subplots=cpu_total
x_total = 60

x = [i for i in range(x_total)]
ylist =[[0]*x_total for i in range(number_of_subplots)]

linelist=[]
for i,v in enumerate(xrange(number_of_subplots)):
    ax1 = subplot(number_of_subplots,1,v+1) # v+1 plots start with 1
    ax1.set_ylim([0, 100])
    ax1.grid()
    line, = ax1.plot(x, ylist[v])
    linelist.append(line)
plt.grid(True)

def cpumonitor(i):
    interval = None
    cpu_perc= psutil.cpu_percent(interval, percpu=True)
    if len(x)>=x_total:
        for yitem in ylist:
            yitem.pop(0)
    else:
        x.append(i)
    for item in range(number_of_subplots):
        ylist[item].append(cpu_perc[item])
    for i,v in enumerate(xrange(number_of_subplots)):
        linelist[v].set_ydata(ylist[v])
    fig.canvas.draw()
    fig.canvas.flush_events()

ani = animation.FuncAnimation(fig, cpumonitor)
plt.show()

0 个答案:

没有答案