[ncl-talk] Bar Chart Plotting Issues

Adam Phillips asphilli at ucar.edu
Thu May 11 17:38:32 MDT 2023


Hi Melissa,
I think there are two things going on.
1- Your input x-axis values (= your x array) go up by 1 for every month of
the year, and then jump by 89. This will result in large blank areas in the
plot as NCL will allocate space for (say) 185513-185600. The solution is to
create a new x-axis array after you've defined x:
x_alt = ispan(0,dimsizes(x)-1,1)

manually set the x-axis tick marks and labels (to show say every 5th year):
res at tmXBLabelMode = "ExplicitLabels"
res at tmXBValues = x_alt(0::60). ; create x-axis tick mark every 5 years
res at tmXBLabels = x(0::60) ; and label them appropriately
res at tmXBMinorValues = x_alt(0::6) create minor tick marks every 6 months;
optional

and then pass in your x_alt array as the x-axis values to plot against your
y array:
plot=gsn_csm_xy(wks,x_alt,y,res)

2 - I would reset your bar width to be set to 1, or, if you don't want
successive month bars touching then set it to say 0.8:
res at gsnXYBarChartBarWidth = 0.8

If you have any further questions on this please respond to ncl-talk.
Best,
Adam

On Wed, May 10, 2023 at 9:52 AM Melissa Lazenby via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:

> Dear NCL Users
>
> I am trying to plot a bar chart with year and months on the X-axis and am
> struggling to do so correctly.
> I would also like to choose some years and months to be grey instead of
> black to show certainty levels. I am trying to make a similar plot to the
> one below:
>
>
> This is what I currently have:
>
>
> Any help would be greatly appreciated!
>
> Kindest Regards
> Melissa
>
>
> Code:
>
> ;************************************************
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> ;************************************************
> begin
>
> ;************************************************
> ; plotting
> ;************************************************
>   wks  = gsn_open_wks("X11","Mpwapwa_Index")
>
>   res                          = True
>   res at gsnMaximize              = True
>   res at tiXAxisString             = "Year & Month"       ; x-axis label
>   res at tiYAxisString             = "Precipitation Index"     ; y-axis label
>   res at gsnXYBarChart = True
>   res at gsnXYBarChartBarWidth = 0.1
>   res at gsnYRefLine           = 0             ; reference line
>   res at tiMainString = "Mpwapwa"
>
>   res at trYMinF                   =  -3.5           ; min level
>   res at trYMaxF                   =  3.5            ; max level
>
>   res at vpWidthF            = 1.5         ; Make the plot wider than it
>   res at vpHeightF           = 0.75        ; is high.
>
>   res at gsnDraw             = False              ; do not draw the plot
>   res at gsnFrame            = False
>
> ;Years and Months
> x = (/185501, 185502, 185503, 185504, 185505, 185506, 185507, 185508,
> 185509, 185510, 185511, 185512, 185601, 185602, 185603, 185604, 185605,
> 185606, 185607, 185608, 185609, 185610, 185611, 185612, 185701, 185702,
> 185703, 185704, 185705, 185706, 185707, 185708, 185709, 185710, 185711,
> 185712, 185801, 185802, 185803, 185804, 185805, 185806, 185807, 185808,
> 185809, 185810, 185811, 185812, 185901, 185902, 185903, 185904, 185905,
> 185906, 185907, 185908, 185909, 185910, 185911, 185912, 186001, 186002,
> 186003, 186004, 186005, 186006, 186007, 186008, 186009, 186010, 186011,
> 186012, 186101, 186102, 186103, 186104, 186105, 186106, 186107, 186108,
> 186109, 186110, 186111, 186112, 186201, 186202, 186203, 186204, 186205,
> 186206, 186207, 186208, 186209, 186210, 186211, 186212, 186301, 186302,
> 186303, 186304, 186305, 186306, 186307, 186308, 186309, 186310, 186311,
> 186312, 186401, 186402, 186403, 186404, 186405, 186406, 186407, 186408,
> 186409, 186410, 186411, 186412, 186501, 186502, 186503, 186504, 186505,
> 186506, 186507, 186508, 186509, 186510, 186511, 186512, 186601, 186602,
> 186603, 186604, 186605, 186606, 186607, 186608, 186609, 186610, 186611,
> 186612, 186701, 186702, 186703, 186704, 186705, 186706, 186707, 186708,
> 186709, 186710, 186711, 186712, 186801, 186802, 186803, 186804, 186805,
> 186806, 186807, 186808, 186809, 186810, 186811, 186812, 186901, 186902,
> 186903, 186904, 186905, 186906, 186907, 186908, 186909, 186910, 186911,
> 186912, 187001, 187002, 187003, 187004, 187005, 187006, 187007, 187008,
> 187009, 187010, 187011, 187012, 187101, 187102, 187103, 187104, 187105,
> 187106, 187107, 187108, 187109, 187110, 187111, 187112, 187201, 187202,
> 187203, 187204, 187205, 187206, 187207, 187208, 187209, 187210, 187211,
> 187212, 187301, 187302, 187303, 187304, 187305, 187306, 187307, 187308,
> 187309, 187310, 187311, 187312, 187401, 187402, 187403, 187404, 187405,
> 187406, 187407, 187408, 187409, 187410, 187411, 187412, 187501, 187502,
> 187503, 187504, 187505, 187506, 187507, 187508, 187509, 187510, 187511,
> 187512, 187601, 187602, 187603, 187604, 187605, 187606, 187607, 187608,
> 187609, 187610, 187611, 187612, 187701, 187702, 187703, 187704, 187705,
> 187706, 187707, 187708, 187709, 187710, 187711, 187712, 187801, 187802,
> 187803, 187804, 187805, 187806, 187807, 187808, 187809, 187810, 187811,
> 187812, 187901, 187902, 187903, 187904, 187905, 187906, 187907, 187908,
> 187909, 187910, 187911, 187912, 188001, 188002, 188003, 188004, 188005,
> 188006, 188007, 188008, 188009, 188010, 188011, 188012, 188101, 188102,
> 188103, 188104, 188105, 188106, 188107, 188108, 188109, 188110, 188111,
> 188112, 188201, 188202, 188203, 188204, 188205, 188206, 188207, 188208,
> 188209, 188210, 188211, 188212, 188301, 188302, 188303, 188304, 188305,
> 188306, 188307, 188308, 188309, 188310, 188311, 188312, 188401, 188402,
> 188403, 188404, 188405, 188406, 188407, 188408, 188409, 188410, 188411,
> 188412, 188501, 188502, 188503, 188504, 188505, 188506, 188507, 188508,
> 188509, 188510, 188511, 188512, 188601, 188602, 188603, 188604, 188605,
> 188606, 188607, 188608, 188609, 188610, 188611, 188612, 188701, 188702,
> 188703, 188704, 188705, 188706, 188707, 188708, 188709, 188710, 188711,
> 188712, 188801, 188802, 188803, 188804, 188805, 188806, 188807, 188808,
> 188809, 188810, 188811, 188812, 188901, 188902, 188903, 188904, 188905,
> 188906, 188907, 188908, 188909, 188910, 188911, 188912, 189001, 189002,
> 189003, 189004, 189005, 189006, 189007, 189008, 189009, 189010, 189011,
> 189012/)
>
> ;Index
> y = (/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, -1, -1, 0.05, 0.05, 0, 0, 0, 0, 0, 0, 0, 0,
> -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.05, 0.05, 0.05, 0.05, 0.05, 0, 0, 0,
> 0, 0, 0, 0, 0.05, 0.05, 0.05, 0.05, 0.05, 0, 0, 0, 0, 0, 0, 0.05, 0.05,
> 0.05, 0.05, 0.05, 0.05, 0, 0, 0, 0, 0, 0, 0, -2, -3, -3, -3, -2, 0, 0, 0,
> 0, 0, 0.05, 0, 0.05, 0.05, 0.05, 0.05, 0.05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0/)
>
>  res at tmXBMode        = "Manual"
>   res at tmXBTickStartF  = 185501
>   res at tmXBTickEndF    = 189012
>   ;res at tmXBTickSpacingF= 120
>   res at tmXBLabelStride = 1
>
>
>  bc = new(432,string)
>  bc= "black"
> time = (/185501,185502,185503, 185504, 185505, 185506, 185507, 185508,
> 185509, 185510, 185511, 185512, 185601, 185602, 185603, 185604, 185605,
> 185606, 185607, 185608, 185609, 185610, 185611, 185612, 185701, 185702,
> 185703, 185704, 185705, 185706, 185707, 185708, 185709, 185710, 185711,
> 185712, 185801, 185802, 185803, 185804, 185805, 185806, 185807, 185808,
> 185809, 185810, 185811, 185812, 185901, 185902, 185903, 185904, 185905,
> 185906, 185907, 185908, 185909, 185910, 185911, 185912, 186001, 186002,
> 186003, 186004, 186005, 186006, 186007, 186008, 186009, 186010, 186011,
> 186012, 186101, 186102, 186103, 186104, 186105, 186106, 186107, 186108,
> 186109, 186110, 186111, 186112, 186201, 186202, 186203, 186204, 186205,
> 186206, 186207, 186208, 186209, 186210, 186211, 186212, 186301, 186302,
> 186303, 186304, 186305, 186306, 186307, 186308, 186309, 186310, 186311,
> 186312, 186401, 186402, 186403, 186404, 186405, 186406, 186407, 186408,
> 186409, 186410, 186411, 186412, 186501, 186502, 186503, 186504, 186505,
> 186506, 186507, 186508, 186509, 186510, 186511, 186512, 186601, 186602,
> 186603, 186604, 186605, 186606, 186607, 186608, 186609, 186610, 186611,
> 186612, 186701, 186702, 186703, 186704, 186705, 186706, 186707, 186708,
> 186709, 186710, 186711, 186712, 186801, 186802, 186803, 186804, 186805,
> 186806, 186807, 186808, 186809, 186810, 186811, 186812, 186901, 186902,
> 186903, 186904, 186905, 186906, 186907, 186908, 186909, 186910, 186911,
> 186912, 187001, 187002, 187003, 187004, 187005, 187006, 187007, 187008,
> 187009, 187010, 187011, 187012, 187101, 187102, 187103, 187104, 187105,
> 187106, 187107, 187108, 187109, 187110, 187111, 187112, 187201, 187202,
> 187203, 187204, 187205, 187206, 187207, 187208, 187209, 187210, 187211,
> 187212, 187301, 187302, 187303, 187304, 187305, 187306, 187307, 187308,
> 187309, 187310, 187311, 187312, 187401, 187402, 187403, 187404, 187405,
> 187406, 187407, 187408, 187409, 187410, 187411, 187412, 187501, 187502,
> 187503, 187504, 187505, 187506, 187507, 187508, 187509, 187510, 187511,
> 187512, 187601, 187602, 187603, 187604, 187605, 187606, 187607, 187608,
> 187609, 187610, 187611, 187612, 187701, 187702, 187703, 187704, 187705,
> 187706, 187707, 187708, 187709, 187710, 187711, 187712, 187801, 187802,
> 187803, 187804, 187805, 187806, 187807, 187808, 187809, 187810, 187811,
> 187812, 187901, 187902, 187903, 187904, 187905, 187906, 187907, 187908,
> 187909, 187910, 187911, 187912, 188001, 188002, 188003, 188004, 188005,
> 188006, 188007, 188008, 188009, 188010, 188011, 188012, 188101, 188102,
> 188103, 188104, 188105, 188106, 188107, 188108, 188109, 188110, 188111,
> 188112, 188201, 188202, 188203, 188204, 188205, 188206, 188207, 188208,
> 188209, 188210, 188211, 188212, 188301, 188302, 188303, 188304, 188305,
> 188306, 188307, 188308, 188309, 188310, 188311, 188312, 188401, 188402,
> 188403, 188404, 188405, 188406, 188407, 188408, 188409, 188410, 188411,
> 188412, 188501, 188502, 188503, 188504, 188505, 188506, 188507, 188508,
> 188509, 188510, 188511, 188512, 188601, 188602, 188603, 188604, 188605,
> 188606, 188607, 188608, 188609, 188610, 188611, 188612, 188701, 188702,
> 188703, 188704, 188705, 188706, 188707, 188708, 188709, 188710, 188711,
> 188712, 188801, 188802, 188803, 188804, 188805, 188806, 188807, 188808,
> 188809, 188810, 188811, 188812, 188901, 188902, 188903, 188904, 188905,
> 188906, 188907, 188908, 188909, 188910, 188911, 188912, 189001, 189002,
> 189003, 189004, 189005, 189006, 189007, 189008, 189009, 189010, 189011,
> 189012/)
>
> selyr = (/185709,185812,185912,186001, 186002,18603,186012,
> 187105,187211,187412,187506,187601,188001,188102,188204/)
> do gg = 0,dimsizes(selyr)-1
>      bc = where(time.eq.selyr(gg),"grey",bc)
>  end do
>
>  res at gsnXYBarChartColors = "bc"
>
>
> plot=gsn_csm_xy(wks,x,y,res)
>
>  draw(plot)
>  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*
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at mailman.ucar.edu
> List instructions, subscriber options, unsubscribe:
> https://mailman.ucar.edu/mailman/listinfo/ncl-talk
>


-- 
Adam Phillips
Associate Scientist IV, Climate Analysis Section
Climate and Global Dynamics Laboratory
National Center for Atmospheric Research
www.cgd.ucar.edu/staff/asphilli/

<http://www.cgd.ucar.edu/staff/asphilli>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230511/1dd27832/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 64237 bytes
Desc: not available
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230511/1dd27832/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 24966 bytes
Desc: not available
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230511/1dd27832/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Outlook-gkrj01ui.png
Type: image/png
Size: 24937 bytes
Desc: not available
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230511/1dd27832/attachment-0002.png>


More information about the ncl-talk mailing list