I'm experimenting with Matplotlib animated charts currently. Having an issue where, using a public dataset, the data isn't animating. I am pulling data from a public CSV file following some of the guidance from this post (which has to be updated a bit for things like the URL of the data). I've tested my Matplotlib installation, and it displays static test data without issue in a variety of formats. Not sure if this is a problem in Pandas getting the data across or something I've missed with the animation.
Code:
import matplotlib.animation as ani
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv'
df = pd.read_csv(url, delimiter=',', header='infer')
df_interest = df.loc[
df['Country/Region'].isin(['United Kingdom', 'US', 'Italy', 'Germany'])
& df['Province/State'].isna()]
df_interest.rename(
index=lambda x: df_interest.at[x, 'Country/Region'], inplace=True)
df1 = df_interest.transpose()
df1 = df1.drop(['Province/State', 'Country/Region', 'Lat', 'Long'])
df1 = df1.loc[(df1 != 0).any(1)]
df1.index = pd.to_datetime(df1.index)
print(df1)
color = ['red', 'green', 'blue', 'orange']
fig = plt.figure()
plt.xticks(rotation=45, ha="right", rotation_mode="anchor") # rotate the x-axis values
plt.subplots_adjust(bottom=0.2, top=0.9) # ensuring the dates (on the x-axis) fit in the screen
plt.ylabel('No of Deaths')
plt.xlabel('Dates')
def buildmebarchart(i=int):
plt.legend(df1.columns)
p = plt.plot(df1[:i].index, df1[:i].values) # note it only returns the dataset, up to the point i
for i in range(0, 4):
p[i].set_color(color[i]) # set the colour of each curve
animator = ani.FuncAnimation(fig, buildmebarchart, interval=50)
plt.show()
Result (a static graph with no data):

UPDATE:
It appears to be this line of pandas code that might be the culprit:
p = plt.plot(df1[:i].index, df1[:i].values)
The df1[:i].index and .values are empty when I insert a breakpoint to print them out.