Python 在matplotlib中动态更新绘图(动画)
我正在尝试使用Tkinter在Matplotlib Python 3.x中动态更新绘图。 它首先显示一个文件对话框,供用户选择.csv文件 以下是.csv文件的示例: 我想打印每一行,然后更新打印并打印下一行 以下是我目前拥有的:Python 在matplotlib中动态更新绘图(动画),python,python-3.x,matplotlib,tkinter,plot,Python,Python 3.x,Matplotlib,Tkinter,Plot,我正在尝试使用Tkinter在Matplotlib Python 3.x中动态更新绘图。 它首先显示一个文件对话框,供用户选择.csv文件 以下是.csv文件的示例: 我想打印每一行,然后更新打印并打印下一行 以下是我目前拥有的: plt.style.use('fivethirtyeight') xs =[] ys = [] csvFile = filedialog.askopenfile(mode='r', filetypes=(("CSV file", "
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()
它确实可以动态绘制数字,但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\u events()方法完成这些事情。我将尝试在这里给出一个一般性的答案,可能需要根据具体的数据和需求进行一些调整。此外,考虑使用Time.Scess()来控制图形更新的速度。
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)
您是否尝试过使用fig.canvas.flush_events()?类似这样的内容:line.set_ydata(row)fig.canvas.draw()fig.canvas.flush_events(下一行)plt.show()…不,我没有,但我会尝试一下。感谢堆:)哦,我想你不必在flush\u事件中输入下一行。我将试着在下面写一个简短的一般性回答,没有具体的数据是很难的。谢谢你的回答。我试着在代码中使用它,图形是绘制的,但它不是动态变化的,这意味着我仍然需要修改代码,对吗?我根据一个简单的excel文件和数字数据编辑了答案。它适用于那种数据结构。您可能需要删除列名或用0到N列的整数替换它们(可能,请检查)。