我在这里绘制可用的netCDF文件:
https://goo.gl/QyUI4J
使用下面的代码, map 如下所示:
但是,我希望海洋呈白色。更好的是,我希望能够指定海洋显示为哪种颜色。如何更改下面的代码来做到这一点?目前,问题在于海洋正在数据规模上标绘。 (请注意,netCDF文件的大小约为3.5 GB)。
import pdb, os, glob, netCDF4, numpy
from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap
def plot_map(path_nc, var_name):
"""
Plot var_name variable from netCDF file
:param path_nc: Name of netCDF file
:param var_name: Name of variable in netCDF file to plot on map
:return: Nothing, side-effect: plot an image
"""
nc = netCDF4.Dataset(path_nc, 'r', format='NETCDF4')
tmax = nc.variables['time'][:]
m = Basemap(projection='robin',resolution='c',lat_0=0,lon_0=0)
m.drawcoastlines()
m.drawcountries()
# find x,y of map projection grid.
lons, lats = get_latlon_data(path_nc)
lons, lats = numpy.meshgrid(lons, lats)
x, y = m(lons, lats)
nc_vars = numpy.array(nc.variables[var_name])
# Plot!
m.drawlsmask(land_color='white',ocean_color='white')
cs = m.contourf(x,y,nc_vars[len(tmax)-1,:,:],numpy.arange(0.0,1.0,0.1),cmap=plt.cm.RdBu)
# add colorbar
cb = m.colorbar(cs,"bottom", size="5%", pad='2%')
cb.set_label('Land cover percentage '+var_name+' in '+os.path.basename(path_nc))
plt.show()
plot_map('perc_crops.nc','LU_Corn.nc')
最佳答案
您需要在maskoceans
数据集上使用 nc_vars
在contourf
之前,插入
nc_new = maskoceans(lons,lats,nc_vars[len(tmax)-1,:,:])
然后使用新屏蔽的数据集调用contourf
,即cs = m.contourf(x,y,nc_new,numpy.arange(0.0,1.0,0.1),cmap=plt.cm.RdBu)
要确定海洋的颜色,您可以将调用调至drawslmask
(如果您要使用白色海洋),也可以在该调用中指定海洋的颜色-例如插入m.drawlsmask(land_color='white',ocean_color='cyan')
。我在下面给出了对您的修改尽可能少的工作代码。取消对
drawslmask
的调用的注释,以查看青绿色的海洋。输出
完整的工作版本的代码
import pdb, os, glob, netCDF4, numpy
from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap, maskoceans
def plot_map(path_nc, var_name):
"""
Plot var_name variable from netCDF file
:param path_nc: Name of netCDF file
:param var_name: Name of variable in netCDF file to plot on map
:return: Nothing, side-effect: plot an image
"""
nc = netCDF4.Dataset(path_nc, 'r', format='NETCDF4')
tmax = nc.variables['time'][:]
m = Basemap(projection='robin',resolution='c',lat_0=0,lon_0=0)
m.drawcoastlines()
m.drawcountries()
# find x,y of map projection grid.
lons, lats = nc.variables['lon'][:],nc.variables['lat'][:]
# N.B. I had to substitute the above for unknown function get_latlon_data(path_nc)
# I guess it does the same job
lons, lats = numpy.meshgrid(lons, lats)
x, y = m(lons, lats)
nc_vars = numpy.array(nc.variables[var_name])
#mask the oceans in your dataset
nc_new = maskoceans(lons,lats,nc_vars[len(tmax)-1,:,:])
#plot!
#optionally give the oceans a colour with the line below
#Note - if land_color is omitted it will default to grey
#m.drawlsmask(land_color='white',ocean_color='cyan')
cs = m.contourf(x,y,nc_new,numpy.arange(0.0,1.0,0.1),cmap=plt.cm.RdBu)
# add colorbar
cb = m.colorbar(cs,"bottom", size="5%", pad='2%')
cb.set_label('Land cover percentage '+var_name+' in '+os.path.basename(path_nc))
plt.show()
plot_map('perc_crops.nc','LU_Corn.nc')
P.S. 这是要测试的大文件!
关于python - 使用 basemap 和python在 map 上绘制海洋,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31996071/