Я анимировал диаграмму, показывающую концентрацию CO2 во времени, также известную как кривая Килинга.
Мой CSV-файл содержит три столбца: термин (в годах назад, например, 0,001), концентрация CO2 (по частям) на миллион, например, 413,53) и значение цвета, например, r, g или b.
Я хочу раскрасить различные части линии, используя значения цвета из файла CSV.
Я считаю, Я должен использовать LineCollection Matplotlib, но пока ничего из того, что я пробовал, не работает.
Суть моего кода в следующем ...
from matplotlib import pyplot as plt
from matplotlib import animation
import pandas as pd
df = pd.read_csv('/home/mark/Documents/Data/ClimateData/antarctica2015co2_v4d.csv')
print(df.head)
Writer = animation.writers['ffmpeg']
writer = Writer(fps=100, metadata=dict(artist='Me'), bitrate=1800)
# First set up the figure, the axis, and the plot element we want to animate
#---------------------------------------------------------------------------
fig, ax = plt.subplots()
plt.xlabel('Term',fontsize=10)
plt.ylabel('CO2',fontsize=10)
plt.xticks(fontsize=8)
plt.yticks(fontsize=8)
line, = ax.plot([], [], lw=1)
# Initialization function: plot the background of each frame
#-----------------------------------------------------------
def init():
line.set_data([], [])
return line,
# Animation function of dataframes list
#---------------------------------------
def animate(i):
plt.xlim(df['XaxisMax'][i], df['XaxisMin'][i])
plt.ylim(df['YaxisMin'][i], df['YaxisMax'][i])
line.set_data(df['Term'][:i], df['CO2'][:i])
return line,
# Call the animator. Set number of frames to the length of your list of dataframes
#---------------------------------------------------------------------------------
anim = animation.FuncAnimation(
fig,
animate,
frames=len(df.index),
init_func=init,
interval=50,
blit=True)
anim.save('/home/mark/Documents/Data/KeelingCurve_Rev1.mp4', writer=writer)