0

I have a growing text file having data like following. It gets updated real-time and the count is increasing continuously.

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

I want to live plot the sum of the count at each minute as the following.

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

The following code is what I have tried.

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()

However, this code gives the following error.

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

Totura
  • 19
  • 1
  • 7

1 Answers1

0

You can groupby time and then sum the counts, replace your function by:

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()
Bruno Mello
  • 4,448
  • 1
  • 9
  • 39