You are here: Home / News & Events / Workshops and Trainings / NCL Workshops / Beispiele und Aufgaben Teil I - XY-Plots
Info
Our docs have moved to https://docs.dkrz.de This portal will be shutdown as soon the last content is migrated.

Beispiele und Aufgaben Teil I - XY-Plots

NCL Skripte zur Erzeugung von XY-Plots.

 

Einfacher XY-Plot:

begin

;-- generate data
  y = random_uniform(-5,5,20)

;-- open a workstation
  wks = gsn_open_wks("png", "part_I_simple_xy_I")

;-- create and draw the plot
  plot = gsn_csm_y(wks, y, False)

end

 

Einfacher XY-Plot mit x- und y- Daten:

begin

;-- generate data
  x = ispan(-20,20,1)
  y = random_uniform(-5,5,dimsizes(x))

;-- open a workstation
  wks = gsn_open_wks("png", "part_I_simple_xy_II")

;-- create and draw the plot
  plot = gsn_csm_xy(wks, x, y, False)

end

 

Mehrere Kurven in einem XY-Plot:

begin

;-- open a data file
  f   = addfile("../../data/EH_OM_A1B_3Dvectors_t1-24.nc","r")

  lat = f->lat

;-- set variable (zonal wind)
  u = f->u                                ;-- set variable u

  data      = new((/2,dimsizes(lat)/), float)  ;-- assign multidimensional array
  data(0,:) = u(0,0,:,{10})               ;-- values at longitude 10 deg.
  data(1,:) = u(0,0,:,{-105})             ;-- values at longitude -105 deg.

;-- open a workstation
  wks = gsn_open_wks("png", "part_I_multiple_curves_xy")

;-- set resources
  res               =  True                ;-- create plot resource object

  res@xyLineColors  = (/"blue", "red"/)    ;-- line colors
  res@xyLineThicknesses = (/2.0,2.0/)      ;-- set line thickness

  res@tiMainString  = \
         "Zonal winds at lon 10 deg (blue) and -105 deg (red)" ;-- draw title

;-- create the plot
  plot = gsn_csm_xy(wks, lat, data, res)

end

 

Verschiedene Linienarten (dash pattern):

begin

;-- open a data file
  f = addfile("../../data/EH_OM_A1B_3Dvectors_t1-24.nc","r")

;-- set variable (zonal wind)
  u   = f->u                                ;-- set variable u
  lat = f->lat                             ;-- set lat varaible
   
  data      = new((/3,dimsizes(lat)/), float)  ;-- assign multidimensional array
  data(0,:) = u(0,0,:,{10})               ;-- values at longitude 10 deg.
  data(1,:) = u(0,0,:,{-105})             ;-- values at longitude -105 deg.
  data(2,:) = u(0,0,:,{-120})             ;-- values at longitude -120 deg.

;-- open a workstation
  wks = gsn_open_wks("png", "part_I_dash_pattern_xy")

;-- set resources
  res               =  True                ;-- create plot resource object

  res@xyLineColors  = (/"blue", "red", "green4"/) ;-- line colors
  res@xyLineThicknesses = (/2.0, 3.0, 3.0/)  ;-- set line thickness
  res@xyDashPatterns    = (/0, 11, 4/)       ;-- 0=solid, 11=dashed, 2=dotted

  res@tiMainString  = "Zonal winds at lon 10 (blue), -105 (red) and -120 (green) deg" 
                                              ;-- draw title

;-- create the plot
  plot = gsn_csm_xy(wks, lat, data, res)

end

 

Zwei unterschiedliche y-Achsen:

begin

;-- open a data file
  f   = addfile("../../data/EH_OM_A1B_3Dvectors_t1-24.nc","r")

;-- set variable (zonal wind)
  t   = f->t(0,0,:,{10})     ;-- set variable t;    y-axis on the left
  slp = f->slp(0,:,{10})     ;-- set variable slp;  y-axis on the right
  x   = f->lat
 
;-- open a workstation
  wks = gsn_open_wks("png", "part_I_two_different_y_axis_xy")

;-- resources for variable left y-axis
  resT                      =  True
  resT@xyLineThicknessF     =  3.0                            ;-- set line thickness
  resT@xyLineColor          =  "red"                          ;-- set line color
  resT@xyDashPatterns       =  0                              ;-- solid line
  resT@tiYAxisString        = t@long_name +"  "+"[solid]"     ;-- axis string

;-- resources for variable right y-axis
  resSLP                    =  True      
  resSLP@xyLineThicknessF   =  3.0                            ;-- set line thickness    
  resSLP@xyLineColor        =  "blue"                         ;-- set line color            
  resSLP@xyDashPatterns     =  1                              ;-- dashed line
  resSLP@tiYAxisString      =  slp@long_name +"  "+"[dash]"   ;-- axis string
  resSLP@tiMainString  = "Temperature and Sea Level Pressure at lon=10 deg" ;-- draw title

;-- create the plot
  plot = gsn_csm_xy2(wks, x, t, slp, resT, resSLP)
  
end

 

Gitterlinien:

begin

;-- open a data file
  f = addfile("../../data/EH_OM_A1B_3Dvectors_t1-24.nc","r")

;-- set variable (zonal wind)
  u = f->u                                ;-- set variable u

  data      = new((/2,dimsizes(u&lat)/), float)  ;-- assign multidimensional array
  data(0,:) = u(0,0,:,{10})               ;-- values at longitude 10 deg.
  data(1,:) = u(0,0,:,{-105})             ;-- values at longitude -105 deg.

;-- open a workstation
  wks = gsn_open_wks("png", "part_I_auxiliary_grid_lines_xy")

;-- set resources
  res                             =  True     ;-- create plot resource object

  res@xyLineColors                = (/"blue", "red"/)  ;-- line colors
  res@xyLineThicknessF            =  2.0      ;-- set line thickness

  res@tmXMajorGrid                =  True     ;-- turn on x-grid
  res@tmXMajorGridThicknessF      =  1.0      ;-- thinner the lines
  res@tmXMajorGridLineDashPattern =  2        ;-- use dashed lines
  
  res@tmYMajorGrid                =  True     ;-- turn on y-grid
  res@tmYMajorGridThicknessF      =  1.0      ;-- thinner the lines
  res@tmYMajorGridLineDashPattern =  2        ;-- use dashed lines

  res@tiMainString  = "Zonal winds at lon 10 (blue) and -105 (red) deg" ;-- draw title

;-- create the plot
  plot = gsn_csm_xy(wks, u&lat, data, res)

end

 

Zeitreihe:

begin
  f      = addfile("../../data/ECHAM5_OM_A1B_2001_0101-1001_2D.nc", "r")
  var    = f->tsurf
  time   = f->time
  
;  system("rm -rf tsurf_fldmean.nc")
;  system("cdo -r -f nc -daymean -fldmean -selvar,tsurf ../data/ECHAM5_OM_A1B_2001_0101-1001_2D.nc ./tsurf_fldmean.nc")
;  sf = addfile("./tsurf_fldmean.nc","r")
;  fldmean = sf->tsurf(:,0,0)

  fldmean = dim_avg_n_Wrap(dim_avg_n_Wrap(var,1),1)
  printVarSummary(fldmean)
  print(min(fldmean))
  print(max(fldmean))

  utc_date = cd_calendar(time,2)
;  utc_date = cd_calendar(fldmean&time,2)
  utc_int  = tointeger(utc_date)

  wks  = gsn_open_wks("png", "part_I_timeseries_xy")

  res                      =  True
  res@xyLineThicknessF     =  3

  res@tmXBMode             = "Explicit"
  res@tmXBValues           =  fldmean&time
  res@tmXBValues           =  fldmean&time
  res@tmXBLabels           =  utc_int
  res@tmXBLabelStride      =  2
  res@tmXBLabelJust        = "CenterRight"
  res@tmXBLabelDeltaF      =  0.15
  res@tmXBLabelAngleF      =  45.
  res@tmXBLabelFontHeightF =  0.015 

  res@trYMinF              =  min(fldmean)
  res@trYMaxF              =  max(fldmean)
  res@tiMainString         = "XY-Plot: temperature averages over time"

  plot = gsn_csm_xy(wks, fldmean&time, fldmean, res)

end

 

Referenzlinie:

begin

  x = fspan(1., 12.,24)                         ;-- generate x values
  y = random_uniform(-1.,1.,24)                 ;-- generate y values

  wks = gsn_open_wks("png","part_I_reference_line_color_above_below_xy") ;-- open workstation

  res                       =  True             ;-- create plotting resource object
  res@tmXBMode              = "Explicit"        ;-- use explicit labels
  res@tmXBValues            =  x                ;-- set x-axis values
  res@tmXBLabelFontHeightF  =  0.015            ;-- x-axis label font size
  res@tmXBLabels            =  (/"Jan","","Feb","","Mar","","Apr","","May","","Jun","",\
                                 "Jul","","Aug","","Sep","","Oct","","Nov","","Dec",""/)

  res@gsnYRefLine           =  0.               ;-- reference line at 0. 
  res@gsnAboveYRefLineColor = "red"             ;-- fill with red above the reference line
  res@gsnBelowYRefLineColor = "blue"            ;-- fill with blue below the reference line

  res@tiMainString          = "XY-Plot: coloring above/below reference line"  ;-- draw title

  plot = gsn_csm_xy(wks,x,y,res)                ;-- create the plot

end

 

Farbfüllung zwischen Kurve und Referenzlinie:

begin

  x = fspan(1., 12.,24)                         ;-- generate x values
  y = random_uniform(-1.,1.,24)                 ;-- generate y values

  wks = gsn_open_wks("png","part_I_fill_color_above_below_xy") ;-- open workstation

  res                       =  True             ;-- create plotting resource object
  res@tmXBMode              = "Explicit"        ;-- use explicit labels
  res@tmXBValues            =  x                ;-- set x-axis values
  res@tmXBLabelFontHeightF  =  0.015            ;-- x-axis label font size
  res@tmXBLabels            =  (/"Jan","","Feb","","Mar","","Apr","","May","","Jun","",\
                                 "Jul","","Aug","","Sep","","Oct","","Nov","","Dec",""/)

  res@gsnYRefLine           =  0.               ;-- reference line at 0. 
  res@gsnAboveYRefLineColor = "red"             ;-- fill with red above the reference line
  res@gsnBelowYRefLineColor = "blue"            ;-- fill with blue below the reference line

  res@tiMainString          = "XY-Plot: coloring above/below reference line"  ;-- draw title

  plot = gsn_csm_xy(wks,x,y,res)                ;-- create the plot

end

 

Farbfüllung zwischen zwei Kurven:

begin

;-- open a data file
  f = addfile("../../data/EH_OM_A1B_3Dvectors_t1-24.nc","r")
  lat = f->lat

;-- set variable (zonal wind)
  u = f->u                                ;-- set variable u

  data      = new((/2,dimsizes(lat)/), float)  ;-- assign multidimensional array
  data(0,:) = u(0,0,:,{10})               ;-- values at longitude 10 deg.
  data(1,:) = u(0,0,:,{-105})             ;-- values at longitude -105 deg.

;-- open a workstation
  wks = gsn_open_wks("png", "part_I_fill_between_lines_xy")

;-- set resources
  res                       =  True       ;-- create plot resource object
  res@xyLineThicknessF      =  2.0        ;-- set line thickness

  res@gsnXYAboveFillColors  = "red"       ;-- fill with red above the reference line
  res@gsnXYBelowFillColors  = "blue"      ;-- fill with blue below the reference line

  res@tiMainString  = "Zonal winds at lon 10 (blue) and -105 (red) deg" ;-- draw title

;-- create the plot
  plot = gsn_csm_xy(wks, lat, data, res)

end

 

XY-Plot mit ASCII-Daten:

begin

;-- read ASCII data
  data_col = asciiread("../../data/ASCII_data.asc", (/12,2/), "integer")

  x = data_col(:,0)                        ;-- get all data (rows) from column 1
  y = data_col(:,1)                        ;-- get all data (rows) from column 2

;-- open a workstation
  wks = gsn_open_wks("png", "part_I_read_ASCII_data_xy")

;-- set resources
  res               =  True                ;-- create plot resource object
  res@tiMainString  = "counts per day"     ;-- draw a title on top of the plot

;-- create the plot
  plot = gsn_csm_xy(wks, x, y, res)

end

 

XY-Plot mit geographischen Daten:

begin

;-- open a data file
  f = addfile("../../data/EH_OM_A1B_3Dvectors_t1-24.nc","r")

;-- set variables
  x = f->lat                               ;-- x-axis values are latitudes
  y = f->u(0,0,:,{45})                     ;-- values nearest to longitude 10 deg.

;-- open a workstation
  wks = gsn_open_wks("png", "part_I_read_data_xy")

;-- set resources
  res               =  True                ;-- create plot resource object
  res@tiMainString  = "Title:  zonal wind" ;-- set title string

;-- create and draw the plot
  plot = gsn_csm_xy(wks, x, y, res)

end

 

Für die Aufgabe:

part_I_read_ASCII_data_xy.ncl

begin

;-- read ASCII data
  data_col = asciiread("../../data/ASCII_data.asc", (/12,2/), "integer")

  x = data_col(:,0)                        ;-- get all data (rows) from column 1
  y = data_col(:,1)                        ;-- get all data (rows) from column 2

;-- open a workstation
  wks = gsn_open_wks("png", "part_I_read_ASCII_data_xy")

;-- set resources
  res               =  True                ;-- create plot resource object

  res@tiMainString  = "counts per day"     ;-- draw a title on top of the plot

;-- create the plot
  plot = gsn_csm_xy(wks, x, y, res)

end

 

Document Actions