我下面有一个脚本,它昨天工作正常,但由于某种原因它今天不行。可能或者可能与我在mac os x中安装anaconda的尝试有关,但是其他脚本工作正常。
import netCDF4 as nc
import matplotlib.pyplot as plt
import numpy as np
from pylab import *
f = nc.Dataset('~/data/ncep/uwnd.mon.mean.nc')
data = f.variables['uwnd']
lon=f.variables['lon']
lat=f.variables['lat']
lev=f.variables['level']
#calculate mean for january only from 1981 (index no starts 396) to 2010 (index no ends 757)
zonal = np.mean(data[396:757:12,:,38,39:43],axis=0)
lon=lon[39:43]
font = {'size' : 9}
plt.rc('font', **font)
#begin plot
clf()
clevs=np.arange(-10.,10.,0.5)
plt.axis([97.5, 105., 1000., 10.])
plt.tick_params(direction='out', which='both')
plt.xlabel('Lon (degrees)')
plt.ylabel('Pressure (mb)')
plt.xticks(np.arange(97.5, 105., .5),'vertical')
plt.yticks([1000, 700, 500, 300, 100, 10])
cs=plt.contourf(lon, lev, zonal, clevs, extend='both',cmap='seismic')
plt.yscale('log')
cax = fig.add_axes([0.99, 0.1, 0.03, 0.8]) #plot colorbar
aa=fig.colorbar(cs,cax=cax,orientation='vertical')
aa.set_label('m/s')
plt.show()
#end
我收到了这个错误:
>$ python plot-crossection-zonal-at5N-test2.py
File "plot-crossection-zonal-at5N-test2.py", line 37, in <module>
cs=plt.contourf(lon, lev, zonal, clevs, extend='both',cmap='seismic')
File "/Users/fadzilnor/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2712, in contourf
ret = ax.contourf(*args, **kwargs)
File "/Users/fadzilnor/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 5325, in contourf
return mcontour.QuadContourSet(self, *args, **kwargs)
File "/Users/fadzilnor/anaconda/lib/python2.7/site-packages/matplotlib/contour.py", line 1428, in __init__
ContourSet.__init__(self, ax, *args, **kwargs)
File "/Users/fadzilnor/anaconda/lib/python2.7/site-packages/matplotlib/contour.py", line 874, in __init__
self._process_args(*args, **kwargs)
File "/Users/fadzilnor/anaconda/lib/python2.7/site-packages/matplotlib/contour.py", line 1441, in _process_args
x, y, z = self._contour_args(args, kwargs)
File "/Users/fadzilnor/anaconda/lib/python2.7/site-packages/matplotlib/contour.py", line 1505, in _contour_args
x, y, z = self._check_xyz(args[:3], kwargs)
File "/Users/fadzilnor/anaconda/lib/python2.7/site-packages/matplotlib/contour.py", line 1535, in _check_xyz
y = np.asarray(y, dtype=np.float64)
File "/Users/fadzilnor/anaconda/lib/python2.7/site-packages/numpy/core/numeric.py", line 460, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: __array__() takes no arguments (1 given)
这个错误到底是什么?
答案 0 :(得分:1)
我终于得到了解决方案, 我只需要放冒号,
lev=f.variables['level'][:]
如下图所示:
f = nc.Dataset('~/data/ncep/uwnd.mon.mean.nc')
data = f.variables['uwnd']
lon=f.variables['lon']
lat=f.variables['lat']
lev=f.variables['level'][:] # ADDED COLON HERE
zonal = np.mean(data[396:757:12,:,38,39:43],axis=0)#,39:43
lon=lon[39:43]
font = {'size' : 9}
plt.rc('font', **font)
clf()
clevs=np.arange(-10.,10.,0.5)
plt.axis([97.5, 105., 1000., 10.])
plt.tick_params(direction='out', which='both')
plt.xlabel('Lon (degrees)')
plt.ylabel('Pressure (mb)')
plt.xticks(np.arange(97.5, 105., .5),label='vertical')
plt.yticks([1000, 700, 500, 300, 100, 10])
cs=plt.contourf(lon, lev, zonal, clevs, extend='both',cmap='seismic')
plt.yscale('log')
cax = plt.axes([0.99, 0.1, 0.03, 0.8]) #plot colorbar
aa=plt.colorbar(cs,cax=cax,orientation='vertical')
aa.set_label('m/s')
plt.show()
但不确定错误消息的含义。很高兴解决这样一个恼人的问题。