use*_*827 5 python plot matplotlib-basemap
我正在绘制netCDF文件:https: //goo.gl/QyUI4J
但是,我希望海洋是白色的.更好的是,我希望能够指定海洋出现的颜色.如何更改下面的代码来做到这一点?现在,问题是海洋正在数据规模上绘制.(请注意,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')
Run Code Online (Sandbox Code Playgroud)
您需要maskoceans在nc_vars数据集上使用
之前contourf,插入此
nc_new = maskoceans(lons,lats,nc_vars[len(tmax)-1,:,:])
Run Code Online (Sandbox Code Playgroud)
然后contourf使用新屏蔽的数据集调用ie
cs = m.contourf(x,y,nc_new,numpy.arange(0.0,1.0,0.1),cmap=plt.cm.RdBu)
Run Code Online (Sandbox Code Playgroud)
要指定海洋颜色,您可以放弃调用,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')
Run Code Online (Sandbox Code Playgroud)
PS这是一个值得测试的大文件!
合法的好解决方案是使用效用函数maskoceans,它接受一个数据数组并屏蔽海洋和湖泊中的所有点。
相反,您可以采取简单的方法。首先绘制您的等高线图,然后使用drawlsmask允许透明颜色的 :
# Colors can be RGBA tuples
m.drawlsmask(land_color=(0, 0, 0, 0), ocean_color='deeppink', lakes=True)
Run Code Online (Sandbox Code Playgroud)
土地是透明的,这可以让等高线图显示出来。