0

似乎无法弄清楚为什么我的图表无法正确显示。我正在尝试使用 netCDF4 和 mpl_toolkits.Basemap 在地图投影上绘制深度。我尝试了几种预测,但都没有奏效。下面是我正在使用的代码:

from netCDF4 import Dataset
from mpl_toolkits.basemap import Basemap
import mpl_toolkits as mplt
import matplotlib.pyplot as plt
import numpy as np

f = Dataset("/geosci/path2data/depth_data.nc")

# extract and label column
lats = f.variables['lat'][:]  # extract/copy the latitude data
lons = f.variables['lon'][:]  # extract/copy the longitude data
depth = f.variables['Band1'][:]
lon_0 = lons.mean()
lat_0 = lats.mean()


#create map projection
m = Basemap(projection = 'stere', width = 5*10**6, height = 35*10**5, resolution = 'l', lat_ts = 40, lat_0 = lat_0, lon_0 = lon_0)

# Plot of global temperature on our random day
fig = plt.figure()
fig.subplots_adjust(left=0., right=1., bottom=0., top=0.9)

# Make the plot continuous
depth_cyclic, lons_cyclic = mplt.basemap.addcyclic(depth, lons) 

# compute map projection coordinates for lat/lon grid
# Create 2D lat/lon arrays for Basemap
lon2d, lat2d = np.meshgrid(lons_cyclic, lats)
# Transforms lat/lon into plotting coordinates for projection
x, y = m(lon2d, lat2d)

# Plot of air temperature with 11 contour intervals
cs = m.contourf(lon2d, lat2d, depth, 11, cmap=plt.cm.Spectral_r)
cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5)
m.plot(x,y)
plt.show()

这是我得到的错误回报。仍在自学编码,需要一点帮助。

ValueError                                Traceback (most recent call last)
<ipython-input-7-7b81b2da3866> in <module>()
     18 
     19 # Plot of air temperature with 11 contour intervals
---> 20 cs = m.contourf(lon2d, lat2d, depth, 11, cmap=plt.cm.Spectral_r)
     21 cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5)
     22 m.plot(x,y)

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc
in with_transform(self, x, y, data, *args, **kwargs)
    519             # convert lat/lon coords to map projection coords.
    520             x, y = self(x,y)
--> 521         return plotfunc(self,x,y,data,*args,**kwargs)
    522     return with_transform
    523 

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc
in contourf(self, x, y, data, *args, **kwargs)
   3669                 data = ma.asarray(data)
   3670                 # combine with data mask.
-> 3671                 mask = np.logical_or(ma.getmaskarray(data),xymask)
   3672                 data = ma.masked_array(data,mask=mask)
   3673                 CS = ax.contourf(x,y,data,*args,**kwargs)

ValueError: operands could not be broadcast together with shapes (12529,15661) (12529,15662)
4

1 回答 1

0

我忘记了分配给其他信息的连续深度。

cs = m.contourf(x, y, depth_cyclic, 11, cmap=plt.cm.Spectral_r)

于 2017-12-05T04:10:12.183 回答