【发布时间】:2020-11-06 22:22:20
【问题描述】:
我正在尝试使用 Tkinter 在 Matplotlib Python 3.x 中动态更新绘图。 首先显示一个文件对话框,供用户选择 .csv 文件。
我想绘制每一行,然后更新绘图并绘制下一行。
这是我目前拥有的:
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 更快,而且我可以控制速度(?)有人可以帮忙吗?
谢谢。
【问题讨论】:
-
您是否尝试过使用 fig.canvas.flush_events()?像这样:line.set_ydata(row) fig.canvas.draw() fig.canvas.flush_events(next row) plt.show() ...
-
不,我没有,但我会尝试一下。谢谢大家:)
-
哦,我认为您不必在 flush_events 中输入下一行。我将尝试在下面写一个简短的通用答案,没有具体数据很难。
标签: python python-3.x matplotlib tkinter plot