Python3.6错误传递数组切片

时间:2017-04-03 13:07:00

标签: python python-3.x matplotlib-basemap python-3.6 rodeo

我试图在函数内使用底图绘制一些数据(obs_ascsplot(d))。我将netcdf4对象传递给该函数。我提取变量并检查形状。然后我只传递子图的2D切片。但是,错误表明我通过了1D而不是。我看不出这是怎么可能的。我已经包含了这个功能并在下面打电话。非常感谢您对正在发生的事情的看法。我在Rodeo IDE中使用Python3.6。

提前致谢

def obs_ascsplot(d):
    fig = plt.figure(2,(11.,8.),dpi=dpi)
    grid_top = ImageGrid(fig, 111, nrows_ncols = (3,2),
                         cbar_location = "bottom",
                         cbar_mode="single",
                         label_mode = "L",
                         share_all=True,
                         cbar_size="3%",
                         cbar_pad="2%",
                         aspect = True,
                         axes_pad=0.2)
    txt = ['(A)','(D)','(B)','(E)','(C)','(F)']
    asky = d.variables['ca_asky'][:]
    csky = d.variables['ca_csky'][:]
    print(type(asky), 'shape = ',asky.shape)
    print(type(csky), 'shape = ',csky.shape)
    lon = d.variables['lon'][:]
    lat = d.variables['lat'][:]
    for g,pos in zip(grid_top,range(6)):
        plt.sca(g)
        print('Ploting figure %d'%(pos+1))
        M = Basemap(projection='hammer',lon_0=0.0,lat_0=0.0,resolution='l')
        M.drawcoastlines()
        M.drawparallels(np.arange(-90.,120.,30.))
        M.drawmeridians(np.arange(0.,400.,60.))
        index = pos//2
        if(pos in [0,2,4]):
            I = M.pcolormesh(lon,lat,asky[index,:,:],vmin=240.,vmax=290.,
                             latlon=True,cmap=mycmap,shading='flat')
            plt.annotate(txt[pos], xy=(0,1), xycoords='axes fraction')
        else:
            I = M.pcolormesh(lon,lat,csky[index,:,:],vmin=240.,vmax=290.,
                             latlon=True,cmap=mycmap,shading='flat')
            plt.annotate(txt[pos], xy=(0,1), xycoords='axes fraction')
        pdb.set_trace()
    cb = grid_top.cbar_axes[0].colorbar(I)
    fig.savefig(fdir+'mhs_obs_as_cs.pdf',dpi=dpi)
    plt.show()
    return None

if __name__ == "__main__":
    #   Main script below
    # get obs and model
    idir = './data/latest/'
    i25  = 'month_means_oifs_mmr_rt_cv_mmr_t511_2007_25_0.nc'
    i30  = 'month_means_oifs_mmr_rt_cv_mmr_t511_2007_30_0.nc'
    d25  = nc.Dataset(idir+i25,'r')
    d30  = nc.Dataset(idir+i30,'r')
    print(d25.variables.keys())
    print(d30.variables.keys())
    x = obs_ascsplot(d25)


odict_keys(['masky', 'month', 'ch', 'lat', 'lon', 'mcsky', 'mce', 'msi', 'mzm', 'ma_asky', 'ma_csky', 'ma_ce', 'ma_si', 'asky', 'months', 'csky', 'ce', 'si', 'zm', 'ca_asky', 'ca_csky', 'ca_ce', 'ca_si'])
odict_keys(['masky', 'month', 'ch', 'lat', 'lon', 'mcsky', 'mce', 'msi', 'mzm', 'ma_asky', 'ma_csky', 'ma_ce', 'ma_si', 'asky', 'months', 'csky', 'ce', 'si', 'zm', 'ca_asky', 'ca_csky', 'ca_ce', 'ca_si'])

<class 'numpy.ma.core.MaskedArray'> shape =  (3, 512, 1024)
<class 'numpy.ma.core.MaskedArray'> shape =  (3, 512, 1024)
Ploting figure 1
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:1623: MatplotlibDeprecationWarning: The get_axis_bgcolor function was deprecated in version 2.0. Use get_facecolor instead.
  fill_color = ax.get_axis_bgcolor()
IndexError: Inconsistant shape between the condition and the input (got (1024,) and (512, 1024))
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-93-be39a3427e68> in <module>()
--> 191     x = obs_ascsplot(d25)
<ipython-input-93-be39a3427e68> in obs_ascsplot(d)
     94         if(pos in [0,2,4]):
     95             I = M.pcolormesh(lon,lat,asky[index,:,:],vmin=pmin,vmax=pmax,
---> 96                              latlon=True,cmap=mycmap,shading='flat')
     97             plt.annotate(txt[pos], xy=(0,1), xycoords='axes fraction')
     98         else:
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py in with_transform(self, x, y, data, *args, **kwargs)
    516             # cylindrical and pseudo-cylindrical projections.
    517             if self.projection in _cylproj or self.projection in _pseudocyl:
--> 518                 x, data = self.shiftdata(x, data)
    519             # convert lat/lon coords to map projection coords.
    520             x, y = self(x,y)
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py in shiftdata(self, lonsin, datain, lon_0)
   4804                 lonsin = np.where(mask,1.e30,lonsin)
   4805                 if datain is not None and mask.any():
-> 4806                     datain = ma.masked_where(mask, datain)
   4807         if datain is not None:
   4808             return lonsin, datain
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/ma/core.py in masked_where(condition, a, copy)
   1913     if cshape and cshape != ashape:
   1914         raise IndexError("Inconsistant shape between the condition and the input"
-> 1915                          " (got %s and %s)" % (cshape, ashape))
   1916     if hasattr(a, '_mask'):
   1917         cond = mask_or(cond, a._mask)
IndexError: Inconsistant shape between the condition and the input (got (1024,) and (512, 1024))

0 个答案:

没有答案