1

我在下面有一个脚本,昨天它运行良好,但由于某种原因,它今天不行。可能与我在我的 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)

这个错误是怎么回事?

4

1 回答 1

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()

不过,不确定错误消息的含义。很高兴解决这样一个烦人的问题。

于 2014-10-15T16:05:42.777 回答