实时绘制熊猫数据帧

时间:2017-05-11 07:27:30

标签: python pandas animation matplotlib 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

简化代码是:

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个数字..... 我对绘图和子图之间的区别感到有点困惑,并认为问题可能与此有关。

sample output

1 个答案:

答案 0 :(得分:4)

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

相反,您希望在while循环之外创建Figure,并且只在加载新数据后刷新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')

编辑1

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

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

编辑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中的列数,即时生成它。