[ncl-talk] Issue with making multiple bar plots on one time series

Melissa Lazenby M.Lazenby at sussex.ac.uk
Fri Feb 3 05:14:45 MST 2023


Dear All NCL Users

I hope you are well.
I am trying to make a bar plot that has 4 bars per year in a times series from 1856-1889.
I have managed to make a plot however the bars are overlaying on one another, and I would like them side by side like the example unique_5.
I have used the fspan option like the example for unique 5 does but it does not seem to be working.
I also am having issues with my x axis labels being the years.
Any help to rectify this issue would be greatly appreciated. Below are the output of the figure and code.

Many thanks!

Kindest Regards
Melissa

[cid:ca412424-01ff-428f-aa69-83fa199caebf]
;*******************************************************
; Mpwapwa_unique_5.ncl
;
; Concepts illustrated:
;   - Drawing multiple bar charts on a page
;   - Drawing three custom legends outside a bar chart
;   - Using bar charts to draw standard deviations for four time series
;   - Drawing a time series plot
;*******************************************************
;
; These files are loaded by default in NCL V6.2.0 and newer
; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"

begin


x = (/1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887, 1888, 1889/)

yIndex = (/0,2,-1,-3,2,0,0,0,0,0,0,0,0,0,0,0,2,0,-1,-1,-2,1,1,-2,-2,1,-1,-3,0,0,0,-3,0,0/)

y20CR3 = (/-1.7049024,  -1.9956352, 0.1017878,  1.5787968,  0.2902762,  0.1615032,  0.7573914,  0.127654184,      0.3391526,  0.18975348, 1.4533978,  0.28738338, 0.8228474,  -1.2142216, 0.67589808, 1.3983746,  1.1087944,  0.58095216, -1.3422622, 0.226781852,      2.173188,   0.6511904,  0.9723054,  -0.4989326, -1.5402072, -0.1535788, -0.226605,  -1.6808838, -0.19809544,      -0.1772666, 0.0769234,  -0.6346154, -1.2222682, -0.0073483/)


;scaled by 10
yLMR = (/0.54,    -0.0624672, -0.0590976, 0.322272,   -1.14912,   0.863136,   0.7776,     1.2528,     -0.0831168, 1.19232,    0.03024,    0.507168,   0.96768,    0.63504,    1.43424,    1.79712,    0.555552,   0.478656,   1.0368,     0.679104,   0.1944,     0.292896,   0.9936,     1.08, 1.18368,    1.26144,    0.631584,   -0.627264,  -0.97632,   -0.325728,  0.00860544, -0.800928,  -0.0360288, 0.186624/)


;scaled by 5
yCMIP5 = (/-0.9094243, 0.5458735, -0.8682191, -1.826278, -0.6501346, 0.1305088, 0.51643, 0.6247665, -0.4082086, 0.3641764, 1.476322, 0.842743, -0.5013026, -0.1281874, 0.4829363, -0.7015711, 0.269588, -0.2834916, -0.8827865, -0.4242322, -1.171241, -0.8289737, -0.4638377, 0.8812776, 0.5154026, -1.191759 ,0.8223722, -1.198702, -0.8878552, -1.724694, -1.62958, 1.02131, -1.440305, -2.299565/)

;yCMIP5 = (/-1.329231, -0.9094243, 0.5458735, -0.8682191, -1.826278, -0.6501346, 0.1305088, 0.51643, 0.6247665, -0.4082086, 0.3641764, 1.476322, 0.842743, -0.5013026, -0.1281874, 0.4829363, -0.7015711, 0.269588, -0.2834916, -0.8827865, -0.4242322, -1.171241, -0.8289737, -0.4638377, 0.8812776, 0.5154026, -1.191759 ,0.8223722, -1.198702, -0.8878552, -1.724694, -1.62958, 1.02131, -1.440305, -2.299565/)


     stdarr = new((/34,4/),"float")
     do hh = 0,33
        stdarr(hh,0) = (/ (yIndex(hh::34)) /)
        stdarr(hh,1) = (/ (y20CR3(hh::34)) /)
        stdarr(hh,2) = (/ (yLMR(hh::34)) /)
      stdarr(hh,3) = (/ (yCMIP5(hh::34)) /)
     end do
     print("Index Data = "+dimsizes(yIndex)+", 20CR3 = "+dimsizes(y20CR3)+", LMR = "+dimsizes(yLMR)+", CMIP5 = "+dimsizes(yCMIP5))

;======================================================================================   
     wks = gsn_open_wks("X11","Mpwapwa_4_Plot")          ; send graphics to PNG file

     sres = True
     sres at vpWidthF = 0.7
     sres at vpHeightF = 0.5
     sres at vpXF = .15
     sres at trYMinF = -3.5
     sres at trYMaxF = 3.5
     sres at gsnDraw = True
     sres at gsnFrame = False
     sres at gsnXYBarChart = True
     sres at gsnXYBarChartBarWidth = 0.20           ; change bar widths
     sres at tmXBMode          = "Explicit"         ; explicit labels
     sres at tmXBValues        =  (/1856,    1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887, 1888, 1889/)
     ;sres at tmXBValues            = (/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34/)
     sres at tmXBLabels = (/"1856","1857","1858",  "1859",     "1860",     "1861",     "1862",     "1863",     "1864",     "1865",     "1866",     "1867",     "1868",     "1869",     "1870",     "1871",     "1872",     "1873",     "1874",     "1875",     "1876",     "1877",     "1878",     "1879",     "1880",     "1881",     "1882",     "1883",     "1884",     "1885",     "1886",     "1887",     "1888",     "1889"/)
     sres at tmXBLabelFontHeightF = 0.0205
     sres at tmXTLabelFontHeightF = 0.0205
     sres at tmYLLabelFontHeightF = 0.0225
     sres at tiMainFontHeightF = 0.025
     sres at tiMainFont = "helvetica"
     sres at tiMainString = "Precipitation Anomalies for all 4 datasets"
     sres at gsnRightString = ""
     sres at tiYAxisString = "Pr Anomalies (mm/day)"
     sres at gsnYRefLine           = 0.              ; reference line

     sres at gsnXYBarChartColors = (/"red"/) 
     plot1 = gsn_csm_xy(wks,fspan(.775,33.775,34),stdarr(:,0),sres)           ; draw each time series
     sres at gsnXYBarChartColors = (/"lightblue"/)                         ; seperately, not
     plot2 = gsn_csm_xy(wks,fspan(.925,33.925,34),stdarr(:,1),sres)           ; advancing the frame
     sres at gsnXYBarChartColors = (/"blue"/)                              ; but tweaking where
     plot3 = gsn_csm_xy(wks,fspan(1.075,34.075,34),stdarr(:,2),sres)          ; each time series is
     sres at gsnXYBarChartColors = (/"green"/)                             ; drawn on the X-axis
     plot4 = gsn_csm_xy(wks,fspan(1.225,34.225,34),stdarr(:,3),sres)

     lbres                    = True          ; labelbar only resources
     lbres at vpWidthF           = 0.2           ; labelbar width
     lbres at vpHeightF          = 0.1           ; labelbar height
     lbres at lbBoxMajorExtentF  = 0.36          ; puts space between color boxes
     lbres at lbFillColors       = (/"green","blue"/)
     lbres at lbMonoFillPattern  = True          ; Solid fill pattern
     lbres at lbLabelFontHeightF = 0.025         ; font height. default is small
     lbres at lbLabelJust        = "CenterLeft"  ; left justify labels
     lbres at lbPerimOn          = False
     lbres at lgPerimColor        = "white"
     labels = (/"CMIP5","20CR3"/)
     gsn_labelbar_ndc(wks,2,labels,0.52,0.23,lbres)   ; draw right labelbar column
      
     lbres at lbFillColors       = (/"lightblue","red"/)
     labels = (/"LMR","Index"/)
     gsn_labelbar_ndc(wks,2,labels,0.17,0.23,lbres)   ; draw left labelbar column
     frame(wks)
end


Dr. Melissa Lazenby
Lecturer in Climate Change
Department of Geography
Chichester 1 C150
University of Sussex

"Education is the most powerful weapon which you can use to change the world" Nelson Mandela

[cid:d803561d-88ff-4fe1-9c8c-2ccee5c3b54c]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230203/395841fe/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 35799 bytes
Desc: image.png
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230203/395841fe/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Outlook-j11uarkt.png
Type: image/png
Size: 24937 bytes
Desc: Outlook-j11uarkt.png
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230203/395841fe/attachment-0001.png>


More information about the ncl-talk mailing list