实时绘制熊猫数据框

doc*_*rer 3 python animation matplotlib pandas real-time-updates

我是新手,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
Run Code Online (Sandbox Code Playgroud)

简化的代码是:

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()
Run Code Online (Sandbox Code Playgroud)

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

样本输出

fsi*_*vic 6

我认为您的代码的问题在于fig, ax = plt.subplots()每次刷新数据时都会调用。这Figure每次都会创建一个新的,因此您会看到新的框架弹出。

相反,您希望创建循环的Figure外部while,并且仅在Axes加载新数据后刷新。

我已经使用您提供的基本示例创建了一个自我更新的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')
Run Code Online (Sandbox Code Playgroud)

编辑 1

我无法重现 raise Warning,但我的猜测是它与pause电话有关。也许尝试交换以下内容并编辑暂停时间。

   ax.plot_date(dframe.index.to_pydatetime(), dframe, marker='', linestyle='solid')
   plt.pause(0.01)
Run Code Online (Sandbox Code Playgroud)

编辑 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)
Run Code Online (Sandbox Code Playgroud)

如果您有更多列,请向colors数组添加更多颜色。或者,根据 中的列数即时生成它dframe