2017-12-02 4 views
1

こんにちは、matplotlibで風をプロットしようとすると、次のエラーが発生します。IndexError:plot winds netcdfの配列のインデックスが多すぎます

は私のコード従ってください:

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** 

解決方法がわかりません。

attached my dataset for my original code!!!!

答えて

1

変数wind_speedている4次元、あなたのファイルのncdump -h

short wind_speed(time, depth, latitude, longitude) ;

として、それを読んで:

var = nc.variables['wind_speed'][0,:,:] 

あなたが3次元に残されています。depth, latitude, longitude、どのcontourf()の件数が多すぎます。そのため、プロットする深さを選択する必要があります。

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

おかげで動作しなかった、.. cnplot = map.contourf(X、Y、VAR [0、:、:、:]、clevs、CMAP = plt.cm.jet) はIndexError:あまりにも多くのアレイ でIndexErrorTraceback(最新のコール最後) () 1 ----> 2 cnplot = map.contourf(X、Y、VAR [0、の指標: (self、indx) /Users/Juan/anaconda/lib/python2.7/site-packages/numpy/ma/core.pyc in __getitem __(self、indx) 3218#それでもまだ正しく設定されていないと再形成のマスクがありません 3219#現在のバージョンに固執する方が簡単です –

+0

あなたの 'var'は3次元です。 'var [0、:、:、:]'とタイプすると、3次元だけでなく、4ではない*エラーが出ます* var [0、:、:] ' – Bart

+0

ありがとう再びバート...私は得ることができません:私は風ベクトルをプロットしたい、私は風速で置き換え別の変数を含むスクリプトサンプルを使用したが動作しません。 ....... >>> cUplot = map.contourf(x、y、var [0、:、]、clevs、cmap = plt.cm.jet) IndexError:配列のインデックスが多すぎます IndexErrorTraceback最後のコールは最後) () ----> 1 cnplot = map.contourf(x、y、var、0、:、:]、clevs、cmap = plt .cm.jet) /Users/Juan/anaconda/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc with_transform(self、x、y、data、* args、** kwargs) –

関連する問題