熊猫每分钟的值总和[重复]

问题描述 投票:0回答:1
我的文本文件越来越大,其数据如下。它得到实时更新,并且数量不断增加。

count,time 1,2020-04-06 21:57:00 2,2020-04-06 21:57:00 3,2020-04-06 21:57:00 4,2020-04-06 21:57:00 5,2020-04-06 21:57:00 6,2020-04-06 21:57:00 7,2020-04-06 21:58:00 8,2020-04-06 21:58:00 9,2020-04-06 21:59:00 10,2020-04-06 21:59:00 11,2020-04-06 21:59:00 12,2020-04-06 22:00:00 13,2020-04-06 22:00:00 14,2020-04-06 22:01:00 15,2020-04-06 22:01:00 16,2020-04-06 22:02:00

我想如下绘制每分钟的计数总和。

2020-04-06 21:57:00 21 2020-04-06 21:58:00 15 2020-04-06 21:59:00 30 2020-04-06 22:00:00 25 2020-04-06 22:01:00 29 2020-04-06 22:02:00 16

下面的代码是我尝试过的。

import pandas as pd import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation plt.style.use('fivethirtyeight') x_vals = [] y_vals = [] index = count() def animate(i): data = pd.read_csv('output.txt') data['time'] = pd.to_datetime(data['time']) data = data.set_index(['time']) x = data.index y1 = data.groupby(pd.TimeGrouper('1Min')).sum() plt.cla() plt.plot(x, y1, label='count') plt.xticks(rotation=90) plt.legend(loc='upper left') plt.tight_layout() ani = FuncAnimation(plt.gcf(), animate, interval=1000) plt.tight_layout() plt.show()

但是,此代码给出以下错误。

ValueError: x and y must have same first dimension, but have shapes (16,) and (6, 1)

python-3.x pandas matplotlib plot real-time
1个回答
0
投票
def animate(i): data = pd.read_csv('output.txt') data['time'] = pd.to_datetime(data['time']) data = .groupby('time')['count'].sum() x = data.index y1 = data['count'] plt.cla() plt.plot(x, y1, label='count') plt.xticks(rotation=90) plt.legend(loc='upper left') plt.tight_layout()
© www.soinside.com 2019 - 2024. All rights reserved.