[英]Dynamically updating plot (Funcanimation) in matplotlib
我正在尝试使用 Z6F8BBEA5A89184EBA80DZA8 在 Matplotlib Python 3.x 中动态更新 plot。 它首先为用户显示一个文件对话框到 select.csv 文件。
我想每行 plot 然后更新 plot 并绘制下一行。
这是我目前拥有的:
plt.style.use('fivethirtyeight')
xs =[]
ys = []
csvFile = filedialog.askopenfile(mode='r', filetypes=(("CSV file", "*.csv"), ("All files", "*.*")), title="Select a CSV file")
csvFile2 = pd.read_csv(csvFile, header=[2])
selectedColumn = csvFile2.iloc[0:, 3:]
selectedArray = selectedColumn.to_numpy() # arrays
def animate():
for row in selectedArray:
plt.clf() #clear current figure
plt.plot(row)
plt.title('Plot')
plt.xlabel('Radar No')
plt.ylabel('Value')
plt.ylim(0.2, 0.9) # Keeping the y axis stays the same during the loop
plt.draw()
plt.pause(0.0078) #0.0078 if the frequency is 128Hz -- Idk about this one
plt.show()
animate()
它会动态执行 plot 数字,但 fps 很慢,大约 5 fps。
因此,我正在寻找另一种方法 Funcanimation,但我不知道如何使用它。 在变量Selectedarray内部是这样的:
[0.489377 0.481563 0.477656... 0.300366 0.294261 0.288156] [0.489866 0.48254 0.478633... 0.300855 0.294994 0.288645] [0.489377 0.481319 0.478144... 0.300122 0.293773 0.288156]
……
我相信使用 Funcanimation 更快,我可以控制速度(?)有人可以帮忙吗?
谢谢你。
有一种方法可以使用 flush_events() 方法来完成这些事情。 我将尝试在这里给出一个一般性的答案,可能需要针对特定的数据和需求进行一些调整。 另外,考虑使用 time.sleep() 来控制图形更新的速度。
import pandas as pd
import matplotlib.pyplot as plt
import time
myarray = pd.read_excel("yourdata.xlsx", index_col=None)
fig, ax = plt.subplots(1)
i=0
for row in myarray.head().itertuples():
print(row)
if i == 0:
line, = ax.plot(row)
else:
line.set_ydata(row)
fig.canvas.draw()
fig.canvas.flush_events()
plt.show()
i += 1
time.sleep(0.5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.