使用底图和python在地图中绘制海洋

时间:2015-08-13 18:42:58

标签: python plot matplotlib-basemap

我正在绘制这里提供的netCDF文件: https://goo.gl/QyUI4J

使用下面的代码,地图如下所示: enter image description here

但是,我希望海洋呈白色。更好的是,我希望能够指定海洋出现的颜色。如何更改下面的代码来做到这一点?现在,问题是海洋正在数据规模上绘制。 (请注意,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')

3 个答案:

答案 0 :(得分:8)

您需要在nc_vars数据集

上使用maskoceans

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以查看青色海洋。

输出

enter image description here

代码的完整工作版本

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。 这是一个值得测试的大文件!!

答案 1 :(得分:4)

合法的好解决方案是使用效用函数maskoceans,它接收数据数组并屏蔽海洋和湖泊中的所有点。

相反,你可以采取简单的方法。首先绘制轮廓图,然后使用drawlsmask,它允许透明颜色:

# Colors can be RGBA tuples
m.drawlsmask(land_color=(0, 0, 0, 0), ocean_color='deeppink', lakes=True)

Land是透明的,可以让轮廓图显示出来。

答案 2 :(得分:-2)

您在地图中看到的颜色与传递给contourcf函数的colormap cm.plt.RdBu有关。您需要更改此颜色映射以获得所需的结果。 Here您可以找到底图色彩图的教程。

相关问题