1樓:chaowei wu
可以考慮繫結乙個時間相關的事件。
如下指令碼可以顯示cpu的實時使用率。
import psutil as p
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
POINTS = 300
fig,ax = plt.subplots()
ax.set_ylim([0, 100])
ax.set_xlim([0, POINTS])
ax.set_autoscale_on(False)
ax.set_xticks()
ax.set_yticks(range(0, 101, 10))
ax.grid(True)
user = [None] * POINTS
nice = [None] * POINTS
sys = [None] * POINTS
idle = [None] * POINTS
l_user, = ax.plot(range(POINTS), user, label = 'User %')
l_nice, = ax.plot(range(POINTS), nice, label = 'Nice %')
l_sys, = ax.plot(range(POINTS), sys, label = 'Sys %')
l_idle, = ax.plot(range(POINTS), idle, label = 'Idle %')
ax.legend(loc = 'upper center'ncol = 4, prop = font_manager.FontProperties(size = 10))
bg = fig.canvas.copy_from_bbox(ax.bbox)
def prepare_cpu_usage():
t = p.cpu_times()
if hasattr(t, 'nice'return [t.user, t.nice, t.system, t.idle]
elsereturn [t.user, 0, t.system, t.idle]
before = prepare_cpu_usage()
def get_cpu_usage():
global before
now = prepare_cpu_usage()
delta = [now[i] - before[i] for i in range(len(now))]
total = sum(delta)
before = now
return [(100.0*dt)/(total+0.1) for dt in delta]
def OnTimer(ax):
global user,nice,sys,idle,bg
tmp = get_cpu_usage()
user = user[1:] + [tmp[0]]
nice = nice[1:] + [tmp[1]]
sys = sys[1:] + [tmp[2]]
idle = idle[1:] + [tmp[3]]
l_user.set_ydata(user)
l_nice.set_ydata(nice)
l_sys.set_ydata(sys)
l_idle.set_ydata(idle)
ax.draw_artist(l_user)
ax.draw_artist(l_nice)
ax.draw_artist(l_sys)
ax.draw_artist(l_idle)
ax.figure.canvas.draw()
timer = fig.canvas.new_timer(interval=100)
timer.add_callback(OnTimer,ax)
timer.start()
plt.show()
截圖如下圖所示,
另外可以參考 Matplotlib for python developers , 書中有乙個wx與matplotlib結合、根據實時資料繪圖的詳盡例子。
2樓:圓核桃
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
# Fixing random state for reproducibility
np.random.seed(196)
#初始資料繪圖
dis = np.zeros(40)
dis2 = dis
fig, ax = plt.subplots()
line, = ax.plot(dis)
ax.set_ylim(-1, 1)
plt.grid(True)
ax.set_ylabel("distance: m")
ax.set_xlabel("time")
def update(frame):
global dis
global dis2
global line讀入模擬
a = np.random.rand()*2-1
time.sleep(np.random.rand()/10)
#繪圖資料生成
dis[0:-1] = dis2[1:]
dis[-1] = a
dis2 = dis
#繪圖line.set_ydata(dis顏色設定
if abs(a) < 0.5plt.setp(line, 'color', 'r', 'linewidth', 2.0)
elseplt.setp(line, 'color', 'b', 'linewidth', 2.0)
return line
ani = animation.FuncAnimation(fig, update,frames=None, interval=100)
plt.show()
有啥需要改的也可以直接看官網參考。
animation - Matplotlib 2.2.0 documentation
3樓:君子Python
已知,matplotlib 也可以畫動態圖的,如下:
import
numpy
asnp
import
matplotlib.pyplot
asplt
plt.
axis([0
,100,0
,1])plt
.ion
()foriin
range
(100):y
=np.random
.random
()plt
.scatter(i
,y)plt
.pause
(0.1
)繪圖結果截圖如下:
對於實時資料繪製座標系曲線圖,希望大家給出更有效的辦法!!
Python如何隨資料更新實時畫圖?
xufive matplotlib提供了animation模組實現動態更新資料,請看下面的例子。import numpy asnp import matplotlib.pyplot asplt from matplotlib.animation import FuncAnimation fig,ax...
什麼是實時數倉?有哪些應用場景?
laiyonghao 這題吧,一看就是不願意搜尋的懶蟲或者是想開個題做廣告的傢伙問的。本來不想答的,但覺得答了這題相當於給自己做個小結,留著以後自己看也行,就答一下吧。先來看第一問。實時數倉,首先是個數倉。資料倉儲搞啥的?一般是通過分析分維度的業務資料來幫助制定計畫和支援決策的。可見它的層次是很高的...
electron開發時,資料如何請求?
主程序請求比較合適,前端請求會有一些限制例如跨域等問題。而在node端這塊想怎麼來怎麼來。渲染執行緒幹好web頁面該幹的事就行了。 趙啟明 推薦使用 fetch API 或者 XHR 直接在渲染層請求,不過會有 file 協議無法使用 cookie 和跨域問題,可能通過 electron 提供的 h...