.brian & Verica:
NCL "tutorial"

XY plot

Set up

  • Many of the functions and routines used for plotting are in the files called "gsn_code.ncl", "gsn_csm.ncl", "shea_util.ncl" and "contributed.ncl", which come along with the instalation of NCL. To use plotting functions one needs to first load these files.

    ; load necessary ncl files:
    load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
    load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
    load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
    load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"


  • To plot data, one first has to open a workstation where the plot will be framed. Workstation can be a frame on screen (x11) or a file (eps and epsi for one page, and ps and pdf for multiple pages). However, here we'll be working only with the x11 (screen).

    ; open workstation
    wks = gsn_open_wks("x11","xy-plot")

  • Plots can be made from data stored in files, or from calculated variables. For simplicity, here we use data stored in the file. NCL supports various formats of data. Here, we only provide examples of NetCDF format.

    ; open the file to read data from it
    fi1 = addfile("rain-profiles.cdf","r")

    ; read in data
    ; 1. coordinates
    time = fi1->time
    zt = fi1->zt

    ; 2. data
    thl = fi1->thetal
    qt = fi1->qt


Basic plot

  • Plotting can be quick with NCL automaticly choosing the settings:

    ; plane x-y plot
    plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,False)

Customized plot

  • One can customize the plots to the publishing quality without too much effort, but lots of patience:

    ; customize plot by setting the resources
    res = True

    • Introduce title
      res@tiMainString = "Title of the plot"
      res@tiMainFontHeightF = 0.025
      plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)


    • Change the lines

      1. Combination of markers and lines
        res@tiMainString = "Combination of markers and lines"
        res@xyMarkLineMode = "MarkLines"
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)


      2. Only markers
        res@tiMainString = "Markers"
        res@xyMarkLineMode = "Markers"
        res@xyMarker = 4
        res@xyMarkerSizeF = .005
        res@xyMarkerThicknessF = 2.
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)


      3. Only lines
        res@tiMainString = "Lines"
        res@xyMarkLineMode = "Lines"
        res@xyDashPattern = 4
        res@xyLineThicknessF = 2.
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)


    • Change the aspect ratio of the plot
      res@tiMainString = "Aspect ratio of the plot"
      res@vpHeightF = .6
      res@vpWidthF = .3
      plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)
      delete(res@xyDashPattern)


    • Customise the axes

      1. Change the titles
        res@tiMainString = "Titles of axes"
        res@tiXAxisString = "~F33~q~F21~~B~l~N~ [K]"
        res@tiYAxisString = "z [m]"
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)


      2. Limit the length
        res@tiMainString = "Limit the length of axes"
        res@trXMaxF = 303.
        res@trXMinF = 288.
        res@trYMaxF = 1200.
        res@trYMinF = 0.
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)


      3. Logaritmic axes
        res@tiMainString = "Axes in a log format"
        res@trXLog = True
        res@trYLog = True
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)
        res@trXLog = False
        res@trYLog = False


      4. Reduce number of labels
        res@tiMainString = "Reduce number of labels on the axes"
        res@tmLabelAutoStride = True
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res


      5. Prescribe tick marks and labels
        res@tiMainString = "Prescribe tick marks and labels on the axes"
        res@tmLabelAutoStride = False
        res@tmXBMode = "Explicit"
        res@tmXBValues = (/289.,302./)
        res@tmXBLabels = res@tmXBValues
        res@tmYLMode = "Explicit"
        res@tmYLValues = (/800./)
        res@tmYLLabels = res@tmYLValues
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)


      6. Remove unnecessary axes
        res@tiMainString = "Remove unnecessary axes"
        res@tmXTOn = False
        res@tmXTBorderOn = False
        res@tmYROn = False
        res@tmYRBorderOn = False
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-1,:),zt,res)


    • Legend

      1. Introduce legend
        res@tiMainString = "Introduce legend"
        res@pmLegendDisplayMode = "Always"   ; options"Never","Always"
        res@pmLegendSide = "Right"   ; options:"Top","Bottom","Right","Left"
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-4:,:),zt,res)


      2. Define legend
        res@tiMainString = "Define legend"
        res@lgAutoManage = False
        res@lgPerimOn = False
        res@xyExplicitLegendLabels = (/"4.5","5.0","5.5","6.0"/)+" h"
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-4:,:),zt,res)


      3. Resize legend
        res@tiMainString = "Resize legend"
        res@pmLegendWidthF = 0.12
        res@pmLegendHeightF = 0.1
        res@lgLabelFontHeightF = .015
        plot = gsn_csm_xy(wks,thl(dimsizes(time)-4:,:),zt,res)