Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器错误:打印时数组索引过多_Python_Matplotlib_Netcdf - Fatal编程技术网

Python 索引器错误:打印时数组索引过多

Python 索引器错误:打印时数组索引过多,python,matplotlib,netcdf,Python,Matplotlib,Netcdf,嗨,当我尝试在matplotlib中绘制风时,我遇到以下错误 按照我的代码: from mpl_toolkits.basemap import Basemap, cm, shiftgrid, addcyclic import matplotlib.pyplot as plt from netCDF4 import * import numpy as np #-- open netcdf file nc = Dataset('/Users/Juan/Documents/python/20

嗨,当我尝试在matplotlib中绘制风时,我遇到以下错误

按照我的代码:

from   mpl_toolkits.basemap import Basemap, cm, shiftgrid, addcyclic
import matplotlib.pyplot as plt
from   netCDF4 import *
import numpy as np

#-- open netcdf file
nc = Dataset('/Users/Juan/Documents/python/2017112900_2017113000_daily-ifremer-L3-MWF-GLO-20171201105757-01.0.nc', mode='r')

#-- read variable
var = nc.variables['wind_speed'][0,:,:]
u10 = nc.variables['eastward_wind'][0,:,:]
v10 = nc.variables['northward_wind'][0,:,:]
lat = nc.variables['latitude'][:]
lon = nc.variables['longitude'][:]

u, lonsout = addcyclic(u10, lon)
v, lonsout = addcyclic(v10, lon)

print "lon[0]: ", lonsout[0], "lon[-1]: ", lonsout[-1]
print "lat[0]: ", lat[0], "lat[-1]: ", lat[-1]

print lonsout[:]
print lat[:]

#-- create figure and axes instances
dpi = 100
fig = plt.figure(figsize=(1100/dpi, 1100/dpi), dpi=dpi)
ax  = fig.add_axes([0.1,0.1,0.8,0.9])

#-- create map
map = Basemap(projection='cyl',llcrnrlat= -90.,urcrnrlat= 90.,\
              resolution='c',  llcrnrlon=-180.,urcrnrlon=180.)

#-- draw coastlines, state and country boundaries, edge of map
map.drawcoastlines()
map.drawstates()
map.drawcountries()

#-- create and draw meridians and parallels grid lines
map.drawparallels(np.arange( -90., 90.,30.),labels=[1,0,0,0],fontsize=10)
map.drawmeridians(np.arange(-180.,180.,30.),labels=[0,0,0,1],fontsize=10)

#-- convert latitude/longitude values to plot x/y values
#x, y = map(*np.meshgrid(lon,lat))

x, y = map(lon,lat)

#-- contour levels
clevs = np.arange(210,320,5)

#-- draw filled contours
cnplot = map.contourf(x,y,var,clevs,cmap=plt.cm.jet)


#-- add colorbar
cbar = map.colorbar(cnplot,location='bottom',pad="10%")      #-- pad: distance between map and colorbar
cbar.set_label('deg K')                                      #-- add colorbar title string

#-- transform vector and coordinate data
veclon = u10.shape[1]/2                    #-- only every 2nd vector
veclat = u10.shape[0]/2                    #-- only every 2nd vector
uproj,vproj,xx,yy = map.transform_vector(u,v,lonsout,lat,veclon,veclat,returnxy=True,masked=True)

#-- create vector plot on map
vecplot = map.quiver(xx,yy,uproj,vproj,scale=600)
qk = plt.quiverkey(vecplot, 0.2, -0.2, 20, '20 m/s', labelpos='W')  #-- position and reference label.

#-- add plot title
plt.title('Winds')

#-- display on screen
#plt.show()
运行整个程序后,我收到错误消息:

**IndexError: too many indices for array
        IndexErrorTraceback (most recent call last)
        <ipython-input-31-d5f74a5df99a> in <module>()
        ----> 1 cnplot = map.contourf(x,y,var,clevs,cmap=plt.cm.jet)
        /Users/Juan/anaconda/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc in with_transform(self, x, y, data, *args, **kwargs)
            534             # convert lat/lon coords to map projection coords.
            535             x, y = self(x,y)
        --> 536         return plotfunc(self,x,y,data,*args,**kwargs)
            537     return with_transform
            538 
        /Users/Juan/anaconda/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc in contourf(self, x, y, data, *args, **kwargs)
           3631                 # only do this check for global projections.
           3632                 if self.projection in _cylproj + _pseudocyl:
        -> 3633                     xx = x[x.shape[0]//2,:]
           3634                     condition = (xx >= self.xmin) & (xx <= self.xmax)
           3635                     xl = xx.compress(condition).tolist()
        IndexError: too many indices for array**
**索引器错误:数组的索引太多
IndexErrorTraceback(最近一次调用last)
在()
---->1 cnplot=map.contourf(x、y、var、clevs、cmap=plt.cm.jet)
/Users/Juan/anaconda/lib/python2.7/site packages/mpl_工具包/basemap/__init__;.pyc与_转换(self、x、y、data、*args、**kwargs)
534#将lat/lon坐标转换为地图投影坐标。
535x,y=self(x,y)
-->536返回plotfunc(self、x、y、data、*args、**kwargs)
537带_变换的返回
538
/contourf中的Users/Juan/anaconda/lib/python2.7/site packages/mpl_工具包/basemap/__init__;.pyc(self、x、y、data、*args、**kwargs)
3631#仅检查全球预测。
3632如果自投影在_cylproj+_pseudocyl中:
->3633 xx=x[x.shape[0]//2,:]

3634条件=(xx>=self.xmin)和(xx变量
风速
有4个维度,
ncdump-h

短风速(时间、深度、纬度、经度)

将其解读为:

var = nc.variables['wind_speed'][0,:,:]
剩下的是三维空间:
深度、纬度、经度
,这对于
轮廓f()
来说太多了

cnplot = map.contourf(x, y, var[0,:,:], clevs, cmap=plt.cm.jet)

感谢没有起作用,…cnplot=map.tourtf(x,y,var[0,:,:,:,:,:],clevs,cmap=plt.cm.jet)索引器:数组索引器的索引太多回溯(最近一次调用)在()1--->2 cnplot=map.tourtf(x,y,var[0,:,,:,:,:],clevs,cmap=plt.cm.jet)/Users/Juan/anaconda/lib/python2.7/site-packages/numpy/ma/core.pyc在u getitem中__(self,indx)3218#如果尚未正确设置被重塑的掩码3219#因此更容易坚持当前版本您的
var
有3个维度;键入
var[0,:,:,:,:]
将给出“索引太多”错误,因为只有3个维度,而不是4个维度。它应该是
var[0,:,:,:,:]
再次感谢巴特…我无法获取:我想绘制风矢量,我使用了一个脚本示例,其中包含另一个变量,我用风速替换了它,但不起作用……。>>>cnplot=map.tourtf(x,y,var[0,:,:,:],clevs,cmap=plt.cm.jet)索引器:在()--->1 cnplot=map.contourf(x,y,var[0,:,:,:],clevs,cmap=plt.cm.jet)/Users/Juan/anaconda/lib/python2.7/site packages/mpl_工具包/basemap/u init_uuuuu.pyc in with_transform(self,x,y,data,*args,**kwargs)亲爱的Bart,你能试着用我的数据集运行脚本吗?啊,Basemap希望
x
y
数组是2D的,所以你需要再次包含
x,y=map(*np.meshgrid(lon,lat))
的部分,然后当我设置不同的
clev
()