[ncl-talk] Plotting XY graph (similar to example 5)

Guido Cioni guidocioni at gmail.com
Thu Sep 28 10:15:27 MDT 2017


Toni,
I had a similar problem the other day.
I would suggest you to use the time_axis_label procedure to create the x-axis instead that doing that on your own. It will take care of setting all the resources and produce the needed variables. Plus, it is definitely easier and more portable. 

Have a look here https://www.ncl.ucar.edu/Document/Functions/User_contributed/time_axis_labels.shtml <https://www.ncl.ucar.edu/Document/Functions/User_contributed/time_axis_labels.shtml>

Example of a plot that I did just using this procedure is attached.

Cheers 

> On 28. Sep 2017, at 18:07, Klemm, Toni <toni at ou.edu> wrote:
> 
> Hi!
> 
> I am trying to plot monthly data of threshold exceedance from a precipitation dataset I created prior. The data is already prepared and from 1980 to 2014 in monthly resolution (420 values). My problem is, the plot doesn’t plot the data per month and year but plots all 12 values on the full year and then jumps to the next year (say 1980 -> 1981 -> 1982) instead of the individual months (basically: 1980/1 -> 1980/2 -> 1980/3 etc.). I did create a float dateF variable as described in example 5 (https://www.ncl.ucar.edu/Applications/Scripts/xy_5.ncl <https://www.ncl.ucar.edu/Applications/Scripts/xy_5.ncl>, plot here: https://www.ncl.ucar.edu/Applications/Images/xy_5_1_lg.png <https://www.ncl.ucar.edu/Applications/Images/xy_5_1_lg.png>) but the plots still didn’t change.
> 
> My script is written using example 5 as a rough template.
> 
> Below is my script, and attached are two zip files with my data to test, and two plots from 1980 to 1989 as it looks right now.
> 
> I would appreciate any help and thank you for your time!
> 
> Best,
> Toni
> 
> 
> Toni Klemm
> Ph.D. Candidate
> The University of Oklahoma
> South Central Climate Science Center
> phone: 405 325 1272
> Contributor at the Early Career Climate Forum <https://www.eccforum.org/>
> www.toni-klemm.de <http://www.toni-klemm.de/> | @toniklemm <https://twitter.com/ToniKlemm>
> 
> 
> ; *************************************************************************************************************
> ; Compare maximum and total number of monthly threshold exceedance days for 95th percentile and 99th percentile
> ; *************************************************************************************************************
> ;
> ; Used as template: https://www.ncl.ucar.edu/Applications/Scripts/xy_5.ncl <https://www.ncl.ucar.edu/Applications/Scripts/xy_5.ncl>
> ;
> 
> begin
> 
> 
>   print("****  Start  ****")
> 
> 
> ; ***********************************************
> ; 1 - READ IN FILES
> ; ***********************************************
> 
>   print("1 - Read in q95 and q99 data")
>   
>   q95_files = systemfunc("ls ~/OBS/PRECIP/daily/threshold/by_month_and_year/q95/nc/*.nc")
>   q95_f = addfiles(q95_files,"r")
>   ListSetType(q95_f,"join")
> 
>   q99_files = systemfunc("ls ~/OBS/PRECIP/daily/threshold/by_month_and_year/q99/nc/*.nc")
>   q99_f = addfiles(q99_files,"r")
>   ListSetType(q99_f,"join")
> 
> ; ***********************************************
> ; 2 - READ IN VARIABLES
> ; ***********************************************
> 
>   print("2 - Read in q95 and q99 variables")
> 
>   max_yes_95 = q95_f[:]->max_yes_95
>   min_max_yes_95 = min(max_yes_95)
>   max_max_yes_95 = max(max_yes_95)
> 
>   total_yes_95 = q95_f[:]->total_yes_95
>   min_total_yes_95 = min(total_yes_95)
>   max_total_yes_95 = max(total_yes_95)
> 
>   max_yes_99 = q99_f[:]->max_yes_99
>   min_max_yes_99 = min(max_yes_99)
>   max_max_yes_99 = max(max_yes_99)
> 
>   total_yes_99 = q99_f[:]->total_yes_99
>   min_total_yes_99 = min(total_yes_99)
>   max_total_yes_99 = max(total_yes_99)
>   
>   date    = q95_f[:]->date
>   dimDate = dimsizes(date)
>   dateF   = new(dimDate,float)
>   do n=0,dimDate-1                  ; date      = 198001
>     yyyy    = date(n)/100           ; yyyy      = 1980
>     mon     = date(n)-yyyy*100      ; mon       = 198001 - (1980*100) = 1
>     dateF(n) = yyyy + (mon-1)/12    ; dateF(n)  = 1980 + (1-1)/12 = 1980.0833
> 
>     print("date(n)  = " + date(n) + "(" + n + ")")
>     print("yyyy     = " + yyyy)
>     print("mon      = " + mon)
>     print("dateF(n) = " + dateF(n) + "(" + n + ")")
>     print("max_yes_95(n) = " + max_yes_95(n) + "(" + n + ")")
>   end do
>   printVarSummary(dateF)
>   
> ;  print(max_yes_95)
> ;  printVarSummary(dateF)
> ;  printVarSummary(total_yes_95)
> ;  printVarSummary(total_yes_99)
> ;  printVarSummary(yyyymm)
> ;  sleep(10)
> 
> 
> ;******************************************
> ; 3A - DEFINE XY PLOT FOR MAXIMA COMPARISON
> ;******************************************
> 
>   print("3A - Define XY plot for maxima comparison")
> 
>   diro = "~/OBS/PRECIP/daily/threshold/by_month_and_year/"
>   wks  = gsn_open_wks ("png",diro + "obs_precip_SHP_daily_yes_no_xy_plot_max") ; send graphics to PNG file
> 
>   res          = True                   ; plot mods desired
>   res at gsnFrame = False                  ; don't advance frame yet      
> 
>   res at vpHeightF= 0.2                    ; change aspect ratio of plot
>   res at vpWidthF = 0.7
> 
>   res at gsnLeftString = "Range q95: " + min_max_yes_95 + " - " + max_max_yes_95 + " days"
> ; map subtitle, printed on the top left
>   res at gsnRightString = "Range q99: " + min_max_yes_99 + " - " + max_max_yes_99 + " days"
> ; map subtitle, printed on the top left
> 
>   res at trYMinF  = 0.0
>                  ; min value on y-axis
>   res at trYMaxF  = 8.0                    ; max value on y-axis
> 
> ; since we stretch the plot, we need to slide it over a bit so it does not
> ; run off the page. we do this by:
> 
> ;  res at vpXF     = 0.1                   ; start plot at x ndc coord 
>   
>   res at tiYAxisString     = "Days per Month"    ; y-axis label      
>   res at tiYAxisFontHeightF = 0.012
>   res at tiMainString      = "Maximum Days per Month > 95~S~th~N~ Percentile and 99~S~th~N~ Percentile" ; title
>   res at tiMainFontHeightF = 0.02
>   res at tiMainFont        = 22
>   
> ; create a reference line and shade values above and below with selected colors. This is shading array dsoid.
>   res at gsnYRefLine       = 0.0             ; create a reference line   
> 
> ;*********************************
> ; polyline parameters used on both plots
> ;*********************************
> 
>   polyres                  = True
>   polyres at gsLineThicknessF = 3.0
> ;  polyres at gsLineColor      = 4.0
> 
> ;******************************************
> ; 3B - CREATE XY PLOT FOR MAXIMA COMPARISON
> ;******************************************
> 
>   print("3B - Create XY plot for Maxima Comparison")
> 
> ;  res at gsnAboveYRefLineColor = "green"                    
>   ; above ref line fill red
> ;  res at gsnBelowYRefLineColor = "blue"                       ; below ref line fill blue
>   plot = gsn_csm_xy(wks,dateF,max_yes_95,res)               ; create plot
>   gsn_polyline(wks,plot,dateF,(/max_yes_99/),polyres)  
>      ; add polyline
>   frame(wks)
> ; now advance frame
> 
>   print("****  Done Maxima  ****")
>   print("")
>   
>   
> ;******************************************
> ; 4A - DEFINE XY PLOT FOR TOTALS COMPARISON
> ;******************************************
> 
>   print("4A - Define XY plot for totals comparison")
> 
>   diro = "~/OBS/PRECIP/daily/threshold/by_month_and_year/"
>   wks  = gsn_open_wks ("png",diro + "obs_precip_SHP_daily_yes_no_xy_plot_totals") ; send graphics to PNG file
> 
>   res          = True                   ; plot mods desired
>   res at gsnFrame = False                  ; don't advance frame yet      
> 
>   res at vpHeightF= 0.2                    ; change aspect ratio of plot
>   res at vpWidthF = 0.7
> 
>   res at gsnLeftString = "Range q95: " + min_total_yes_95 + " - " + max_total_yes_95 + " days"
> ; map subtitle, printed on the top left
>   res at gsnRightString = "Range q99: " + min_total_yes_99 + " - " + max_total_yes_99 + " days"
> ; map subtitle, printed on the top left
> 
>   res at trYMinF  = 0.0
>                  ; min value on y-axis
>   res at trYMaxF  = 800.0                    ; max value on y-axis
> 
> ; since we stretch the plot, we need to slide it over a bit so it does not
> ; run off the page. we do this by:
> 
> ;  res at vpXF     = 0.1                   ; start plot at x ndc coord 
>   
>   res at tiYAxisString     = "Days per Month"    ; y-axis label      
>   res at tiYAxisFontHeightF = 0.012
>   res at tiMainString      = "Total Days per Month > 95~S~th~N~ Percentile and 99~S~th~N~ Percentile" ; title
>   res at tiMainFontHeightF = 0.02
>   res at tiMainFont        = 22
>   
> ; create a reference line and shade values above and below with selected colors. This is shading array dsoid.
>   res at gsnYRefLine       = 0.1             ; create a reference line   
> 
> ;*********************************
> ; polyline parameters used on both plots
> ;*********************************
> 
>   polyres                  = True
>   polyres at gsLineThicknessF = 3.0
> ;  polyres at gsLineColor      = 4.0
> 
> ;******************************************
> ; 4B - CREATE XY PLOT FOR TOTALS COMPARISON
> ;******************************************
> 
>   print("4B - Create XY plot for totals comparison")
> 
> ;  res at gsnAboveYRefLineColor = "green"                    
>   ; above ref line fill red
> ;  res at gsnBelowYRefLineColor = "blue"                       ; below ref line fill blue
>   plot = gsn_csm_xy(wks,dateF,total_yes_95,res)               ; create plot
>   gsn_polyline(wks,plot,dateF,(/total_yes_99/),polyres)  
>      ; add polyline
>   frame(wks)
> ; now advance frame
>   
> 
>   print("****  Done Totals  ****")
> 
> end
> 
> 
> 
> <obs_precip_SHP_daily_yes_no_xy_plot_max.png>
> <obs_precip_SHP_daily_yes_no_xy_plot_totals.png>
> <q95.zip><q99.zip>_______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk


Guido Cioni
http://guidocioni.altervista <http://guidocioni.altervista/>.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170928/ace01f54/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: plot_data.ncl
Type: application/octet-stream
Size: 2296 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170928/ace01f54/attachment.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170928/ace01f54/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: time_series_zoom.png
Type: image/png
Size: 123981 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170928/ace01f54/attachment.png>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170928/ace01f54/attachment-0002.html>


More information about the ncl-talk mailing list