DKRZ PyNGL example vector plot#

Example:

from __future__ import print_function
import Ngl,Nio

#-----------------------------------------------------------------------
#-- Function: main
#-----------------------------------------------------------------------
def main():

    #-- open a file and read variables
    f    = Nio.open_file("rectilinear_grid_2D.nc", "r")

    u    = f.variables["u10"]
    v    = f.variables["v10"]
    ua   = f.variables["u10"][0,:,:]
    va   = f.variables["v10"][0,:,:]

    lat  = f.variables["lat"]
    lon  = f.variables["lon"]
    nlon = len(lon)
    nlat = len(lat)

    #-- open a workstation
    wks = Ngl.open_wks("png","plot_vector_on_map")

    #-- resource settings
    vcres                  =  Ngl.Resources()
    vcres.nglFrame         =  False

    vcres.vfXArray         =  lon[::3]
    vcres.vfYArray         =  lat[::3]

    vcres.vcMinFracLengthF =  0.3           #-- length of smallest vector
    vcres.vcRefLengthF     =  0.05          #-- length of reference vector
    vcres.vcRefMagnitudeF  =  20.0          #-- define vector ref mag
    vcres.vcRefLengthF     =  0.035         #-- define length of vec ref

    vcres.mpFillOn         =  True
    vcres.mpOceanFillColor = "Transparent"
    vcres.mpLandFillColor  = "Gray90"
    vcres.mpInlandWaterFillColor = "Gray90"

    #-- create the plot
    plot = Ngl.vector_map(wks,ua[::3,::3],va[::3,::3],vcres)

    #-- write variable long_name and units to the plot
    txres               = Ngl.Resources()
    txres.txFontHeightF = 0.014

    Ngl.text_ndc(wks,f.variables["u10"].attributes['long_name'],0.16,0.8,txres)
    Ngl.text_ndc(wks,f.variables["u10"].attributes['units'],    0.95,0.8,txres)

    #-- advance the frame
    Ngl.frame(wks)

    Ngl.end()

if __name__ == '__main__':
    main()

Result:

PyNgl TRANS vectors w400