在 matplotlib 中动态更新图(Funcanimation) [英] Dynamically updating plot (Funcanimation) in matplotlib

查看:96
本文介绍了在 matplotlib 中动态更新图(Funcanimation)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Tkinter 在 Matplotlib Python 3.x 中动态更新绘图.它首先显示一个文件对话框,供用户选择 .csv 文件.

I am trying to do a dynamically updating plot in Matplotlib Python 3.x using Tkinter. It starts with displaying a file dialog for the user to select the .csv file.

以下是 .csv 文件的示例:

Here's the example of the .csv file:

我想绘制每一行然后更新绘图绘制下一行.

I want to plot each row then the plot is updated & plotting the next row.

这是我目前拥有的:

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.

It does plot the numbers dynamically, but the fps is so slow, about 5 fps.

因此,我正在寻找另一种方法,Funcanimation,但我不确定如何使用它.在变量 Selectedarray 中是这样的:

Therefore, I am looking for another method, Funcanimation, but I am not sure how to use it. Inside the variable Selectedarray is something like this:

[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]

[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 会更快,而且我可以控制速度(?)任何人都可以帮忙.

I believe using Funcanimation is faster and I could control the speed(?) Could anyone pls help.

谢谢.

推荐答案

有一种方法可以使用 flush_events() 方法来做这些事情.我会在这里尝试给出一个一般性的答案,可能需要针对特定​​的数据和需求进行一些调整.另外,考虑使用 time.sleep() 来控制图形更新的速度.

There's a way doing these things with the flush_events() method. I'll try to give a general answer here, may need to adapt a little bit for the specific data and needs. Also, consider using time.sleep() to control for the speed of figure update.

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)

这篇关于在 matplotlib 中动态更新图(Funcanimation)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆