实时绘制 pandas 数据框

2024-03-11

我是新来的matplotlib并尝试显示我通过函数 read_API() 从 api 下载的三个变量的最后一小时数据的实时图。数据位于带有 DateTimeIndex 的 pandas 数据框中。 例如:

In: dframe.head()
Out:
                                 A          B         C
timestamp                                                            
2017-05-11 16:21:55        0.724931  0.361333   0.517720  
2017-05-11 16:22:25        0.725386  0.360833   0.518632
2017-05-11 16:22:55        0.725057  0.361333   0.521157
2017-05-11 16:23:25        0.724402  0.362133   0.520002

简化的代码是:

import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
while True:
    dframe = read_API()
    dframe['timestamp'] = dframe['timestamp'] + pd.DateOffset(hours=timezone)
    dframe = dframe.set_index('timestamp')
    end = dframe.index.max()
    start= end.to_datetime() - dt.timedelta(hours=1)
    dframe = dframe.loc[start:end]
    plt.ion()
    fig, ax = plt.subplots()
    plt.pause(0.0001)
    ax.plot_date(dframe.index.to_pydatetime(), dframe,marker='', linestyle='solid')
    plt.draw()

它每隔几秒钟就会生成更新的绘图,但是: 1)每个图都出现在一个新窗口中(称为图1,图2,图3......)。我想要一个单独的窗口,其中的绘图会覆盖前一个窗口 2)当每个图出现时,它是空白的。然后出现另一个空白的,然后是另一个,然后第一个完成,依此类推。实际绘图落后了大约3位数...... 我对情节和子情节之间的区别有点困惑,并认为问题可能与此有关。


我认为你的代码的问题是你调用fig, ax = plt.subplots()每次刷新数据时。这创建了一个新的Figure https://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure每次,您都会看到新的框架弹出。

相反,您想要创建Figure https://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure在你之外的while循环,并且只刷新Axes https://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes一旦你加载了新数据。

我使用了您提供的基本示例来创建一个自动更新Figure https://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.

import pandas as pd
import matplotlib.pyplot as plt 
import datetime as dt

data = [ 
    {'timestamp': '2017-05-11 16:21:55', 'A': 0.724931, 'B': 0.361333, 'C': 0.517720},
    {'timestamp': '2017-05-11 16:22:25', 'A': 0.725386, 'B': 0.360833, 'C': 0.518632},
    {'timestamp': '2017-05-11 16:22:55', 'A': 0.725057, 'B': 0.361333, 'C': 0.521157},
    {'timestamp': '2017-05-11 16:23:25', 'A': 0.724402, 'B': 0.362133, 'C': 0.520002},
]
df = pd.DataFrame(data) 
df.set_index("timestamp")

plt.ion()
fig, ax = plt.subplots()
while True:
    dframe = df.copy()
    dframe['timestamp'] = pd.to_datetime(dframe['timestamp']) + pd.DateOffset(hours=2)
    dframe = dframe.set_index('timestamp')
    end = dframe.index.max()
    start= end.to_datetime() - dt.timedelta(hours=1)
    dframe = dframe.loc[start:end]
    plt.pause(0.0001)
    ax.plot_date(dframe.index.to_pydatetime(), dframe, marker='', linestyle='solid')

Edit 1

我无法重现所提出的Warning https://docs.python.org/2/library/exceptions.html#exceptions.Warning,但我的猜测是它与pause https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.pause.html#matplotlib.pyplot.pause称呼。也许尝试交换以下内容并编辑暂停时间。

   ax.plot_date(dframe.index.to_pydatetime(), dframe, marker='', linestyle='solid')
   plt.pause(0.01)

Edit 2

修复颜色非常简单。定义您的调色板,然后从中进行选择。

colors = ['r', 'g', 'b']

plt.ion()
fig, ax = plt.subplots()
while True:
    dframe = df.copy()
    # Your data manipulation
    # ...
    dframe = dframe.loc[start:end]
    for i, column in enumerate(dframe.columns):
        ax.plot(dframe.index.to_pydatetime(), dframe[column], color=colors[i], marker=None, linestyle='solid')   
    plt.pause(0.1)

如果您有更多列,请向其中添加更多颜色colors大批。或者,根据中的列数动态生成它dframe.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

实时绘制 pandas 数据框 的相关文章

随机推荐