使用日期时间和 matplotlib 绘图

Plotting with date times and matplotlib

所以,我正在使用 this website to (try) to make stick plots of some netCDF4 data. There is an excerpt of my code below. I got my data from here 中的函数。

stick_plot(time,u,v) 函数与我链接的网站中显示的完全一样,这就是为什么我没有在下面显示该函数的副本。

当我 运行 我的代码出现以下错误。关于如何解决这个问题的任何想法?

AttributeError: 'numpy.float64' object has no attribute 'toordinal'

来自 netCDF4 文件的时间描述:

<type 'netCDF4._netCDF4.Variable'>
float64 time(time)
    long_name: time
    standard_name: time
    units: days since 1900-01-01 00:00:00Z
    axis: T
    ancillary_variables: time_quality_flag
    data_min: 2447443.375
    data_max: 2448005.16667
unlimited dimensions: 
current shape = (13484,)
filling off

这是我的代码的摘录:

进口:

import matplotlib.pyplot as plot
import numpy as np
from netCDF4 import Dataset
import os
from matplotlib.dates import date2num
from datetime import datetime

正在尝试生成图表:

path = '/Users/Kyle/Documents/Summer_Research/east_coast_currents/'
currents = [x for x in os.listdir('%s' %(path)) if '.DS' not in x]

for datum in currents:
    working_data = Dataset('%s' %(path+datum), 'r', format = 'NETCDF4')
    u = working_data.variables['u'][:][:100]
    v = working_data.variables['v'][:][:100]
    time = working_data.variables['time'][:][:100]
    q = stick_plot(time,u,v)
    ref = 1
    qk = plot.quiverkey(q, 0.1, 0.85, ref,
                      "%s N m$^{-2}$" % ref,
                      labelpos='N', coordinates='axes')

    _ = plot.xticks(rotation=70)    

乔金顿回答了我的问题。 netCDF4 文件将时间作为 datetime 对象读取。我所要做的就是将 date2num(time) 替换为 time,这样就解决了所有问题。