gpt4 book ai didi

python - 无法识别的错误类型 : __array__( )

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:38 27 4
gpt4 key购买 nike

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

这个错误是怎么回事?

最佳答案

终于找到解决办法了我只需要输入冒号,

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

但不确定错误消息的含义。很高兴能解决这么烦人的问题。

关于python - 无法识别的错误类型 : __array__( ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26384385/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com