I have the following data, here is a section of it
[{'Date': '2017 Q1', 'Saudi Arabia': 3.5080621303094413, 'Oman': 2.0803722435647836, 'Yemen': 1.9636651338439473, 'Israel': 3.0247692259733565}, {'Date': '2017 Q2', 'Saudi Arabia': 3.03137342597358, 'Oman': 2.2666875108328357, 'Yemen': 2.0820441357351513, 'Israel': 3.145231552094236}, {'Date': '2017 Q3', 'Saudi Arabia': 2.4309916593024394, 'Oman': 2.4635716453158922, 'Yemen': 2.326399413964078, 'Israel': 2.792350006532546}, {'Date': '2017 Q4', 'Saudi Arabia': 3.699283062258509, 'Oman': 3.1202643793473914, 'Yemen': 2.924974137360855, 'Israel': 3.5534406207725384}, {'Date': '2018 Q1', 'Saudi Arabia': 4.685752914561016, 'Oman': 3.7256945856760573, 'Yemen': 3.64807891731718, 'Israel': 4.3811754907135745}]
Except I have the 'Date' set as my index in my df.
I have tried to make an animated line chart using the following:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
file_path= r"C:\Users\Willi\OneDrive\Desktop\Data_for_chart.xlsx"
Title_for_chart = "Middle East inflation"
df = pd.read_excel(file_path)
df = df.T
header = df.iloc[0]
df = df[1:]
df.columns = header
df = df.apply(pd.to_numeric, errors = 'coerce')
#Getting the values so can easily adjust the axis
max_value = df.max().max() + df.max().max()*0.2
min_value = df.min().min() - df.min().min()*0.2
#plotting to see how it would look
dates_vals = list(df.index)
plt.plot(dates_vals, df.iloc[:,:])
plt.show()
##Setting up for animation
%matplotlib
fig, ax = plt.subplots(figsize=(12, 6))
lines = [ax.plot([], [], linestyle='-')[0] for _ in range(df.shape[1])]
def init():
for line in lines:
line.set_data([], [])
return lines
def animate(i):
for j, line in enumerate(lines):
line.set_data(dates_vals[:i], df.iloc[:i, j])
return lines
ani = animation.FuncAnimation(fig=fig, func=animate, frames=len(df), init_func=init, blit=True, repeat=True)
I have set it up like this as then regardless of the number of quarters or number of countries, the chart will work as it comes from an excel file that varies. However, it works when I do plt.show(), but have been unable to get it to animate?
Any suggestions would be greatly appreciated.