我有以下数据,这是其中的一部分
[{“日期”:“2017 年第一季度”,“沙特阿拉伯”:3.5080621303094413,“阿曼”:2.0803722435647836,“也门”:1.9636651338439473,“以色列”:3.024769225973第3565章 '日期':'2017年第二季度','沙特阿拉伯':3.03137342597358,'阿曼':2.2666875108328357,'也门':2.0820441357351513,'以色列':3.145231552094236},{'日期':'2017年第三季度','沙特阿拉伯' :2.4309916593024394,'阿曼':2.4635716453158922,'也门' :2.326399413964078,“以色列”:2.792350006532546},{“日期”:“2017 年第四季度”,“沙特阿拉伯”:3.699283062258509,“阿曼”:3.1202643793473914,“也门”: 2.924974137360855,'以色列':3.5534406207725384},{'日期' :“2018 年第一季度”、“沙特阿拉伯”:4.685752914561016、“阿曼”:3.7256945856760573、“也门”:3.64807891731718、“以色列”:4.3811754907135745}]
除了我在 df 中将“日期”设置为索引。
我尝试使用以下方法制作动画折线图:
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)
我已经这样设置了,无论季度数量或国家数量如何,图表都会工作,因为它来自不同的 Excel 文件。但是,当我执行 plt.show() 时它可以工作,但无法使其动画化?
如有任何建议,我们将不胜感激。
这是该代码的工作版本
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
data = [{'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}]
df = pd.DataFrame(data)
df.set_index('Date', inplace=True)
df = df.apply(pd.to_numeric, errors = 'coerce')
max_value = df.max().max() + df.max().max()*0.2
min_value = df.min().min() - df.min().min()*0.2
dates_vals = list(df.index)
plt.plot(dates_vals, df.iloc[:,:])
date_indices = np.arange(len(dates_vals))
plt.show()
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):
# Use date_indices instead of dates_vals
line.set_data(date_indices[:i], df.iloc[:i, j])
return lines
# Set the x labels correctly
ax.set_xlim(0, len(date_indices) - 1)
ax.set_ylim(min_value, max_value)
ani = animation.FuncAnimation(fig=fig, func=animate, frames=len(df) + 1, init_func=init, blit=True, repeat=True)
ax.set_xticks(date_indices)
ax.set_xticklabels(dates_vals, rotation=45)
# Show the animation
plt.show()
我的改变是:
plt.show()
以显示动画matplotlib.units.ConversionError: Failed to convert value(s) to axis units: ['2017 Q1', '2017 Q2', '2017 Q3', '2017 Q4']
我将 dates_vals
从字符串转换为数字索引,并在动画中使用这些索引。def animate(i):
for j, line in enumerate(lines):
line.set_data(date_indices[:i], df.iloc[:i, j])
return lines
ax.set_xticks(date_indices)
ax.set_xticklabels(dates_vals, rotation=45)
如果您在 Jupyter 笔记本或 Google colab 中工作并且动画不起作用,请尝试将其另存为文件,如下所示
ani.save('animation.gif', writer='imagemagick')