[ncl-talk] Plotting 2 histograms side by side (not overlapping)

Adam Phillips asphilli at ucar.edu
Thu Jan 12 09:49:14 MST 2017


Hi Katie,
Over the years I have transitioned from using gsn_histogram for
binning/plotting to using it only for the binning and then plotting things
myself using gsn_csm_xy. The latter method allows easier customization than
using gsn_histogram on its' own. I would think this method would be best
for the type of plot you are after.

Here's some example code:
; use gsn_histogram to bin the data and set x-axis coordinates
  hres = True
  hres at gsnDraw = False
  hres at gsnFrame = False

  hres at gsnHistogramBinIntervals = fspan(-.8,.8,17)
  plot = gsn_histogram(wks,control,hres)
  fin_rt_ctl = plot at NumInBins*1.
  fin_rt_ctl!0 = "time"
  fin_rt_ctl&time = fspan(-.75,.75,16) ; assign where on x-axis bins
                                                     ; are centered.
  delete(plot)

  plot = gsn_histogram(wks,sim0,hres)
  fin_rt_sim0 = plot at NumInBins*1.
  fin_rt_sim0!0 = "time"
  fin_rt_sim0&time = fspan(-.7,.8,16) ; assign where on x-axis bins
                                                     ; are centered,
shifting from control
  delete(plot)                                   ; time set above

; next, set up the plot resources for the bar chart plot.

  res = True
  res at gsnDraw = False
  res at gsnFrame = False
  res at gsnYRefLine = 0.0
  res at gsnXYBarChart = True
  res at trYMinF = 0.0
  res at trXMinF = -.9
  res at trXMaxF = .9
  res at gsnXYBarChartBarWidth = 0.04 ; set bar widths

  res at gsnXYBarChartColors = (/"black"/)
  plot = gsn_csm_xy(wks,fin_rt_ctl&time,fin_rt_ctl,res)

  res at gsnXYBarChartColors = (/"blue"/)
  plot2 = gsn_csm_xy(wks,fin_rt_sim0&time,fin_rt_sim0,res)
  overlay(plot,plot2)
  draw(plot)
  frame(wks)

The above should at least get you started on what you need to do. You will
likely want to set the x-axis tick marks explicitly as well. (See the
bottom panel coding of example number two here:
http://www.ncl.ucar.edu/Applications/tickmarks.shtml#ex2 )

Hope that helps. If you have any further questions please respond to the
ncl-talk email list.
Adam















On Thu, Jan 12, 2017 at 9:14 AM, Dagon, Katie <kdagon at fas.harvard.edu>
wrote:

> Hello all,
>
> Does anyone know how to plot 2 histograms side by side instead of
> overlapping (as is done by default with the gsnHistogramCompare function)?
> I have tried using "res at gsnHistogramSideBySide = True”, however that
> shifts the distributions slightly to the left, so the values do not line up
> with the correct labels on the x axis. I have not been able to determine
> why this happens, though it may be that SideBySide assumes it will have at
> least 3 datasets, instead of 2.
>
> Ideally, I would like the distributions to sit side by side with some
> white space on either side of the x axis tick marks.
>
> Thanks in advance for any help. I have attached two plot examples when
> using histogram compare versus sidebyside. The compare figure shows the
> data in its correct position with respect to the x axis, and the
> side-by-side figure is shifted to the left. I am using NCL version 6.2.1.
>
> Katie
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>


-- 
Adam Phillips
Associate Scientist,  Climate and Global Dynamics Laboratory, NCAR
www.cgd.ucar.edu/staff/asphilli/   303-497-1726

<http://www.cgd.ucar.edu/staff/asphilli>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170112/91626dc6/attachment.html 


More information about the ncl-talk mailing list