[問題] 請問使用plt.ion / plt.ioff

作者: nicha115 (cha)   2019-11-22 22:19:32
各位高手好,
我目前要畫一個即時數據的動畫
本來要使用animation套件
可是因為我的數據是一直要重新讀取的
所以我使用plt.ion / plt.ioff的方法
加上使用多線程(threading)方式
可是呢,
畫圖會嚴重lag
想請問是我使用多線程正常?
還是我的程式碼有問題呢?
i = 0
obs = Observer() # 這個是建立監控物件,用途是監控一個資料架新生成之file
obs2 = Observer() # 同上
wat = FileWatcher() # 監控用途之一
wat2 = FileWatcher2()
obs.schedule(wat, path=f'{file_path}/{toDay}')
obs2.schedule(wat2, path=f'{file_path2}/{toDay}')
obs.start() # Start watching (一個線程)
obs2.start()
try:
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)
plt.ion()
while True:
ax.clear()
ax2.clear()
if len(data) <300:
y1 = data['AI_0 PEgF'][:]
y2 = data['AI_1 PEgF'][:]
y3 = data['AI_2 PEgF'][:]
yy1 = data2['AI_0 PEgF'][:]
yy2 = data2['AI_1 PEgF'][:]
yy3 = data2['AI_2 PEgF'][:]
x = np.arange(0, len(data))/600
xx = np.arange(0, len(data2))/600
ax.plot(x, y1)
ax.plot(x, y2)
ax.plot(x, y3)
ax2.plot(xx, yy1)
ax2.plot(xx, yy2)
ax2.plot(xx, yy3)
plt.pause(0.1)
#i += 1
else:
x = data.iloc[-300:].index.values/600
y1 = data['AI_0 PEgF'][-300:]
y2 = data['AI_1 PEgF'][-300:]
y3 = data['AI_2 PEgF'][-300:]
xx = data2.iloc[-300:].index.values/600
yy1 = data2['AI_0 PEgF'][-300:]
yy2 = data2['AI_1 PEgF'][-300:]
yy3 = data2['AI_2 PEgF'][-300:]
ax.plot(x, y1)
ax.plot(x, y2)
ax.plot(x, y3)
ax2.plot(xx, yy1)
ax2.plot(xx, yy2)
ax2.plot(xx, yy3)
plt.pause(0.1)
if len(data) > 1000:
data = data[-300:]
data2 = data2[-300:]
i += 1
plt.ioff()
plt.show()
作者: andy086 (andy)   2019-11-24 13:29:00
雖然我沒用過這個,但會lag會不會是記憶體不夠啊?
作者: nicha115 (cha)   2019-11-24 17:05:00
我也搞不太清楚。但我覺得我數據量也不算大試過單獨畫圖(不開線程讀file和data)速度正常我第一次使用threading概念,不知是否用得不好

Links booklink

Contact Us: admin [ a t ] ucptt.com