因此,我正在使用这个网站上的函数来制作一些
当我运行代码时,我收到以下错误。有什么办法可以避免这种情况吗?
尝试生成图形:
netCDF4
数据的棒图。 以下是我的代码摘录。 我从这里获取了数据。
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)
date2num
。date2num
需要datetime
对象。但是你很幸运,你的 NetCDF 文件中的时间字段似乎已经完全符合date2num
要转换的格式。在stick_plot
函数中将date2num(time)
替换为time
,然后你就可以继续了。 - Joe Kington