Я все еще новичок в Python, так что терпите меня, если это нубский вопрос.
Я пытаюсь анимировать график концентраций CO2 с течением времени, он же Кривая Килинга.
Мой CSV-файл содержит два столбца: термин (в годах назад, например, 0,001) и концентрация CO2 (в частях на миллион, например, 413,53).
мой код (основными элементами которого были беззастенчиво заимствовано) выдает ошибку KeyError 0, когда доходит до вызова Funcanimation для Animate ().
Любые мысли будут с благодарностью приняты, так как мои поиски в Google по поиску решений уже вычеркнули пробел.
Мой код следующий ...
from matplotlib import pyplot as plt
from matplotlib import animation
import pandas as pd
import numpy as np
df = pd.read_csv('/home/mark/Documents/Data/ClimateData/antarctica2015co2_v5a_stripped.csv')
print(df.head)
Writer = animation.writers['ffmpeg']
writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800)
# First set up the figure, the axis, and the plot element we want to animate
#---------------------------------------------------------------------------
fig = plt.figure()
ax = plt.axes(
xlim=(8000000, 0),
ylim=(np.min(df)['CO2'], np.max(df)['CO2']))
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):
line.set_data(df[i]['Term'], df[i]['CO2'])
return line,
# Call the animator, animate every 300 ms
# 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=300,
blit=True)
anim.save('/home/mark/Documents/Data/KeelingCurve.mp4', writer=writer)
Я работаю Python 3.7.1, Anaconda 1.9.12 и Spyder 3.3.6 IDE.