First, we define our graphical output (wks) type, size and name.
In [41]:
wks_type = "png" ;-- graphic output type
wks_type@wkWidth = 1200 ;-- width of PNG file
wks_type@wkHeight = 1200 ;-- height if PNG file
wks = gsn_open_wks(wks_type,"plot_tickmark_resources_xy")
Now, we have to set some general resources for the plot.
In [42]:
res = True
res@gsnDraw = False
res@gsnFrame = False
Define the x-axis title string, font and size.
In [43]:
res@tiXAxisString = "time" ;-- x-axis title string
res@tiXAxisFont = 30 ;-- x-axis title font (courier-bold)
res@tiXAxisFontHeightF = 0.014 ;-- x-axis title font size
Ok, lets do it for the y-axis title, too.
In [44]:
res@tiYAxisString = "y" ;-- y-axis title string
res@tiYAxisFont = 30 ;-- y-axis title font (courier-bold)
res@tiYAxisFontHeightF = 0.014 ;-- y-axis title font size
Change the default axis border line color and thickness.
In [45]:
res@tmBorderLineColor = "blue" ;-- x- and y-axis line color
res@tmBorderThicknessF = 8. ;-- x- and y-axis line thickness
Change the default bottom x-axis label size, color and tickmark thickness.
In [46]:
res@tmXBLabelFontHeightF = 0.012 ;-- bottom x-axis label font size
res@tmXBLabelFontColor = "red" ;-- bottom x-axis label color
res@tmXBMajorLineColor = "red" ;-- bottom x-axis major tickmark color
res@tmXBMinorLineColor = "red" ;-- bottom x-axis minor tickmark color
res@tmXBMajorThicknessF = 5. ;-- bottom x-axis major tickmarks thickness
res@tmXBMinorThicknessF = 5. ;-- bottom x-axis minor tickmarks thickness
We're doing now the same for the left y-axis.
In [47]:
res@tmYLLabelFontHeightF = 0.012 ;-- left y-axis label font size
res@tmYLLabelFontColor = "blue" ;-- left y-axis label color
res@tmYLMajorLineColor = "blue" ;-- left y-axis major tickmark color
res@tmYLMinorLineColor = "blue" ;-- left y-axis minor tickmark color
res@tmYLMajorThicknessF = 5. ;-- left y-axis major tickmarks thickness
res@tmYLMinorThicknessF = 5. ;-- left y-axis minor tickmarks thickness
Define the viewport size and position.
In [48]:
res@vpXF = 0.1 ;-- set viewport x-position
res@vpYF = 0.89 ;-- set viewport y-position
res@vpWidthF = 0.85 ;-- set viewport width
res@vpHeightF = 0.38 ;-- set viewport height
In this example we are using dummy data and we have to define the time values for the x-axis.
In [49]:
time = yyyymm_time(2000,2009,"integer") ;-- creates a 1D array containing year-month (yyyymm) values
;!!!
;!!! The time array values are now 200001, 200002, 200003,..., 200012, 200101,... !!!
;!!! --------------
;!!! These leaps are the reason why the xy-plot line looks like a stair!
;!!!
ntime = dimsizes(time) ;-- number of time steps
Generate the dummy data, here simple linear increasing values.
In [50]:
y = ispan(1,ntime,1) ;-- generate data array
y!0 = "time" ;-- set named dimension time
y&time = time ;-- reference time coordinate variable
If we want to control the view of the xy-plot we have to set some xy-resources.
In [51]:
res@xyLabelMode = "Custom" ;-- xy-plot line label mode
res@xyExplicitLabels = "my label" ;-- label the xy-plot line
res@xyLineLabelFontHeightF = 0.018 ;-- size of the label
res@xyLineThicknessF = 2.0 ;-- xy-plot line thickness
res@xyLineColor = "red" ;-- xy-plot line color
res@xyLineDashSegLenF = 0.3 ;-- distance between xy-plot line labels
Define the plot title string and its size.
In [52]:
res@tiMainFontHeightF = 0.022 ;-- title font size
res@tiMainString = "wrong use of the time coordinate variable (yyyymm)" ;-- title string
Create the first plot in memory. We do this to generate a second plot which should be in the same frame as the first one using a panel function.
In [53]:
plot1 = gsn_csm_xy(wks,time,y,res)
Now, we want to prepare the second plot.
Generate the time index and year arrays for explicit x-axis use.
In [54]:
inttime = ispan(1,ntime,1) ;-- create an index array for x-axis dimension time
years = str_get_cols(tostring(time),0,3) ;-- retrieve the years
Generate the time axis labels.
In [55]:
tlabels = new(ntime,"string") ;-- create new array for time x-axis labels
tlabels = "" ;-- initialize tlabels array
yr = years(0) ;-- init yr
tlabels(0) = yr ;-- first label
do i=1,ntime-1
if(years(i) .ne. yr) then
tlabels(i) = years(i) ;-- we want only the first year entries of years array
yr = years(i) ;-- next year
end if
end do
Change the default bottom x-axis labels.
In [56]:
res@tmXBMode = "Explicit" ;-- explicit setting of tick marks and labels
res@tmXBValues = inttime(::12) ;-- locations for major tick marks
res@tmXBMinorValues = inttime(::3) ;-- locations for minor tick marks (tmXBMode "Ecplicit")
res@tmXBLabels = tlabels(::12) ;-- labels of major tick marks
Change the plot title string.
In [57]:
res@tiMainString = "correct use of the time coordinate variable (yyyymm)" ;-- title string
Create the second plot in memory.
In [58]:
plot2 = gsn_csm_xy(wks,inttime,y,res)
We want to display both plots in the same frame using the gsn_panel function.
The panel resources increase the space between the plots a little bit and define the panel title string.
In [59]:
pres = True
pres@gsnPanelYWhiteSpacePercent = 5 ;-- more space between the plots
pres@gsnPanelMainString = "Data vs. Time" ;-- panel title string
Create the panel plot (2 rows x 1 column) and advance the frame
In [60]:
gsn_panel(wks,(/plot1,plot2/),(/2,1/),pres)
Show the PNG plot file.