在X ax上使用日期的FuncAnimation问题

时间:2017-02-22 08:23:31

标签: python matplotlib

我正在尝试通过移动条创建一个按日期降水的电影。它适用于x ax上的浮点数或整数(' JD'),但不适用于日期(' DateTime')(第38/39号,第45/46号,第52/53行) 。我做错了什么?

import numpy as np
import scipy as sc
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib import animation, rc
from IPython.display import HTML
import datetime
import matplotlib.dates as mdates

#import waterlevel data and locations
dateparse = lambda xd: [pd.datetime.strptime(daa, '%d/%m/%Y %H:%M') for daa in xd]
WL = pd.read_csv('test.csv', parse_dates=['DateTime'], date_parser=dateparse, dayfirst=True)
#WL = pd.DataFrame(waterlevels)

#precipitation
maxPH = 10 #round(float(np.max(PH)),3)
minPH = 1#round(float(np.min(PH)),3)

#data for movie with varying frames
aframes = (len(WL.loc[:,'DateTime'])) - 1
print ('Amount of waterlevel measurement points: ', aframes)
print ('Measurements from ', WL.loc[0,['DateTime']], 'to', WL.loc[aframes,['DateTime']])

"""MOVIE .AVI for varying precipitation """
#set up figure, the axes and the plot element to animate
fig, (ax2) = plt.subplots(nrows=1,ncols=1,figsize=(15,15),sharey=False,sharex=False)
#title and subgraph information
ax2.grid()
#precipiation
ax2.plot(WL.loc[:,['DateTime']],WL.loc[:,['regen [mm]']], linewidth = 0.5, c ='blue', label = 'Rain per hour')
#ax2.plot(WL.loc[:,['JD']],WL.loc[:,['regen [mm]']], linewidth = 0.5, c ='blue', label = 'Rain per hour')

#setup waterlevel frames
line, = ax2.plot([],[], c = 'red', lw=5) 

def init():
    xprec = WL.loc[0,['DateTime','DateTime']]
    #xprec = WL.loc[0,['JD','JD']]
    yprec = WL.loc[0,['phmin','phmax']]
    line.set_data(xprec,yprec)
    return line3

def animate(i):
        xprec = WL.loc[i,['DateTime','DateTime']]
        #xprec = WL.loc[i,['JD','JD']]
        yprec = WL.loc[i,['phmin','phmax']]
        line.set_data(xprec,yprec)
        return line

# call the animator. blit=True means: only re-daw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, frames = (aframes + 1), init_func = init)
#aframes must be aframes!!!!!!

anim.save('testmov_precipitation2.mp4', fps=20, extra_args=['-vcodec', 'libx264'])
plt.show()

csv文件的内容:

DateTime,Date2,Time,regen [mm],phmin,phmax,JD 10/01/2017 01:00,10 / 01 / 2017,01:00,1,1,10,1 10/01/2017 02:00,10 / 01 / 2017,02:00,2,1,10,2 10/01/2017 03:00,10 / 01 / 2017,03:00,4,1,10,3 10/01/2017 04:00,10 / 01 / 2017,04:00,6,1,10,4 10/01/2017 05:00,10 / 01 / 2017,05:00,1,1,10,5 10/01/2017 06:00,10 / 01 / 2017,06:00,3,1,10,6 10/01/2017 07:00,10 / 01 / 2017,07:00,8,1,10,7 10/01/2017 08:00,10 / 01 / 2017,08:00,9,1,10,8 10/01/2017 09:00,10 / 01 / 2017,09:00,10,1,10,9 10/01/2017 10:00,10 / 01 / 2017,10:00,2,1,10,10 10/01/2017 11:00,10 / 01 / 2017,11:00,5,1,10,11 10/01/2017 12:00,10 / 01 / 2017,12:00,8,1,10,12 2017/01/10 13:00,10 / 01 / 2017,13:00,1,1,10,13 2017/01/10 14:00,10 / 01 / 2017,14:00,2,1,10,14 10/01/2017 15:00,10 / 01 / 2017,15:00,3,1,10,15 10/01/2017 16:00,10 / 01 / 2017,16:00,4,1,10,16 10/01/2017 17:00,10 / 01 / 2017,17:00,5,1,10,17 10/01/2017 18:00,10 / 01 / 2017,18:00,8,1,10,18 10/01/2017 19:00,10 / 01 / 2017,19:00,7,1,10,19

0 个答案:

没有答案