4.b Plot type - contour

PyNGL provides plot functions to create line and contour plots. This notebook starts with a simple line contour plot and ends with a filled contour plot and its labelbar.

The next lines should be known, now, but this time we use the 2D data file tsurf.nc.


In [1]:
from IPython.display import Image
import xarray as xr
import numpy as np
import Ngl

ds = xr.open_dataset('./data/tsurf.nc')

tsurf = ds.tsurf[0,::-1,:]         #-- first time step, reverse latitude
lat   = ds.lat[::-1]               #-- reverse latitudes
lon   = ds.lon[:]                  #-- all longitudes

wks = Ngl.open_wks('png', 'plot_contour_tsurf.png')

res = Ngl.Resources()

res.sfXArray  =  ds.lon.values
res.sfYArray  =  ds.lat.values

For contour plots there are a lot of resources available to define the settings. But we begin with the default settings of PyNGL to create our first contour line plot. The data to be plotted is the first time step.


In [2]:
plot = Ngl.contour_map(wks, tsurf, res)

Image(filename='plot_contour_tsurf.png')


Out[2]:

Change the contour plot increment to 5 and add a title to the plot.


In [3]:
Ngl.delete_wks(wks)
wks = Ngl.open_wks('png', 'plot_contour_tsurf.png')

res = Ngl.Resources()
res.tiMainString = 'Variable: ' + tsurf.name

res.cnLevelSelectionMode = "ManualLevels" #-- define levels
res.cnMinLevelValF  = 250.                #-- minimum contour value
res.cnMaxLevelValF  = 315.                #-- maximum contour value
res.cnLevelSpacingF =   5.                #-- contour increment

res.sfXArray = ds.lon.values
res.sfYArray = ds.lat.values

plot = Ngl.contour_map(wks, tsurf, res)

Image(filename='plot_contour_tsurf.png')


Out[3]:

There are now too many lines, the map outline and the contour lines. We can fill the land with a gray color and without the map outlines.


In [4]:
Ngl.delete_wks(wks)
wks = Ngl.open_wks('png', 'plot_contour_tsurf.png')

res = Ngl.Resources()
res.tiMainString = 'Variable: ' + tsurf.name

res.cnLevelSelectionMode   = "ManualLevels" #-- define levels
res.cnMinLevelValF         = 250.           #-- minimum contour value
res.cnMaxLevelValF         = 315.           #-- maximum contour value
res.cnLevelSpacingF        =   5.           #-- contour increment

res.mpOutlineOn            =  False         #-- turn on map outlines
res.mpFillOn               =  True          #-- turn map fill on
res.mpOceanFillColor       = "Transparent"  #-- set ocean fill color to transparent
res.mpLandFillColor        = "Gray90"       #-- set land fill color to gray
res.mpInlandWaterFillColor = "Gray90"       #-- set inland water fill color to gray

res.sfXArray = ds.lon.values
res.sfYArray = ds.lat.values

plot = Ngl.contour_map(wks, tsurf, res)

Image(filename='plot_contour_tsurf.png')


Out[4]:

That's better.

To zoom into the global map to have a better view at the data, we have to use other resources of the mp resource group.


In [5]:
Ngl.delete_wks(wks)
wks = Ngl.open_wks('png', 'plot_contour_tsurf.png')

res = Ngl.Resources()
res.tiMainString = 'Variable: ' + tsurf.name

res.cnLevelSelectionMode   = "ManualLevels" #-- define levels
res.cnMinLevelValF         = 250.           #-- minimum contour value
res.cnMaxLevelValF         = 315.           #-- maximum contour value
res.cnLevelSpacingF        =   5.           #-- contour increment

res.mpFillOn               =  True          #-- turn map fill on
res.mpOceanFillColor       = "Transparent"  #-- set ocean fill color to transparent
res.mpLandFillColor        = "Gray90"       #-- set land fill color to gray
res.mpInlandWaterFillColor = "Gray90"       #-- set inland water fill color to gray

res.mpLimitMode            = "LatLon"       #-- limit map via lat/lon
res.mpMinLatF              =  30.           #-- select region to be plotted
res.mpMaxLatF              =  60.
res.mpMinLonF              = -15.
res.mpMaxLonF              =  25.

res.sfXArray = ds.lon.values
res.sfYArray = ds.lat.values

plot = Ngl.contour_map(wks, tsurf, res)

Image(filename='plot_contour_tsurf.png')


Out[5]:

The map resolution is set to LowRes by default, this is good enough for a global map. We change the map resolution to MediumRes from the map data base Earth..4 and decrease the contour increment again.


In [6]:
Ngl.delete_wks(wks)
wks = Ngl.open_wks('png', 'plot_contour_tsurf.png')

res = Ngl.Resources()
res.tiMainString = 'Variable: ' + tsurf.name

res.cnLevelSelectionMode   = "ManualLevels" #-- define levels
res.cnMinLevelValF         = 250.           #-- minimum contour value
res.cnMaxLevelValF         = 315.           #-- maximum contour value
res.cnLevelSpacingF        =   1.           #-- contour increment

res.mpDataSetName          = "Earth..4"     #-- set map data base version
res.mpDataBaseVersion      = "MediumRes"    #-- MediumRes = Ncarg4_1

res.mpFillOn               =  True          #-- turn map fill on
res.mpOceanFillColor       = "Transparent"  #-- set ocean fill color to transparent
res.mpLandFillColor        = "Gray90"       #-- set land fill color to gray
res.mpInlandWaterFillColor = "Gray90"       #-- set inland water fill color to gray

res.mpLimitMode            = "LatLon"       #-- limit map via lat/lon
res.mpMinLatF              =  30.           #-- select region to be plotted
res.mpMaxLatF              =  60.
res.mpMinLonF              = -15.
res.mpMaxLonF              =  25.

res.sfXArray = ds.lon.values
res.sfYArray = ds.lat.values

plot = Ngl.contour_map(wks, tsurf, res)

Image(filename='plot_contour_tsurf.png')


Out[6]: