NCL Home> Application examples> Plot techniques || Data files for some examples

Example pages containing: tips | resources | functions/procedures

Bar Charts

Bar charts in this context are simply XY plots that are drawn with bars for each X,Y point. There is a separate page for creating histograms.

Multi-colored bar charts: example 6

Pattern-filled bar charts: example 7

bar_1.ncl: This shows a series of bar charts.

The first frame sets gsnXYBarChart to True, which causes each Y value to be represented by a bar pointing upward. The base of each bar is the minimum Y value or the value of trYMinF, if set. Each bar is centered about its corresponding X value.

The second frame additionally sets gsnXYBarChartOutlineOnly to True, which causes each Y value to be represented by a horizontal bar, creating an outline plot. There are no individual bars drawn here. Here, each vertical line is exactly at the corresponding X value, and NOT centered about it like gsnXYBarChart.

The third frame sets gsnYRefLine to 0, causing all the positive Y values to be drawn as bars pointing upward, and all negative Y values to be drawn as bars pointing downward.

bar_2.ncl: Demonstrates coloring values above and below a reference line with a particular color.

gsnAboveYRefLineColor = "red", and gsnBelowYRefLineColor = "blue", are the two resources required to shade the bars above and below the reference line.

bar_3.ncl: Demonstrates changing the size of the bars.

By default, the thickness of each bar is delta-x. You can make the bars smaller than this by setting gsnXYBarChartBarWidth to a smaller number. In this instance, delta-x is 0.66 and we have changed the width to 0.1. Note that at this point it becomes hard to see the bar colors, because the bars are pretty thin.

bar_4.ncl: Demonstrates turning off bar outline around each bar. This is useful when you have a lot of data points because the outline is drawn after the color fill, and a black line and can obscure it.

xyLineColors = (/"red","blue"/), changes the outline color for the above and below color filled areas respectively.

bar_5.ncl: Multiple bar chart on one page.
bar_6.ncl: Demonstrates how to make individual bars different colors within a bar chart that has a reference line. The colors can be chosen separately for the above and below bars (first plot), or sequentially regardless of orientation (second plot).

gsnAboveYRefLineBarColors controls the colors of the bars above the reference line. This is an array. The colors will repeat. gsnBelowYRefLineBarColors controls the colors below the reference line.

gsnXYBarChartColors2 will assign a sequence of colors regardless of bar orientation.

bar_7.ncl: A bar chart w/o a reference line, and individual colors. The second frame adds a labelbar.

gsnXYBarChartColors controls the colors of the bars.

bar_8.ncl: Demonstrates how to make individual bars different colors and patterns within a bar chart that has a reference line. Colors are distributed separately for bars above and below the reference line (first plot), or regardless of orientation of the bars (second plot).

gsnAboveYRefLineBarPatterns controls the patterns of the bars above the reference line. This is an array. The patterns will repeat. gsnBelowYRefLineBarPatterns controls the patterns below the reference line.

gsnXYBarChartPatterns2 will apply the patterns regardless of the orientation of the bars.

There are several patterns to choose from.

unique_5.ncl: This script creates a single bar chart showing 4 different timeseries with four different colors. The labelbar is created by using gsn_labelbar_ndc.
polyg_10.ncl: This example shows how to draw various polylines and polygons on a several generic tickmark backgrounds to create a series of bar charts. The gsn_add_polyline and gsn_add_polygon functions are used to create the polylines and polygons and gsn_panel is used to panel all the plots on one frame.
bar_10.ncl: This script shows how to create a horizontal series of filled bars. A blank plot was created first using gsn_blank_plot), and the bars attached using using gsn_add_polygon).
bar_11.ncl: This script shows how to panel multiple bar charts and add a custom legend (using labelbars).

This example is similar to example 9 above, except it uses the overlay procedure to overlay the individual bar plots. Finally, it uses gsn_panel to panel the four sets of plots.

bar_12.ncl: This script shows how to create four sets of "floating" bars given a set of minimum and maximum values for each set of bars. The trick is to draw the plot twice with the same bar color, first with the minimum values, and then with the maximum values. Each set of bars has its own gsnYRefLine value.

bar_13.ncl: This script shows a variation on creating "floating" bars, grouping each set of bars by a single month.

The data are read in from a dummy data file that contains min, max, and average values for seven different models.

bar_14.ncl: This script shows how to draw an XY curve on top of a bar chart.

Two separate plots are created, and then they are drawn in the same space by making sure the vpXF, vpYF, vpWidthF, and vpHeightF resources are the same for both.

bar_15.ncl: This script shows how to draw a bar chart with the bars colored according to ranges of values. The first frame colors the bars using gsnXYBarChartColors2. The second frame colors the bars using calls to gsn_add_polygon. This allows the top of the bars to follow the shape of the XY curve, instead of being flat.

time_axis_labels is used add nicely-formatted time labels to the X axis.

bar_16.ncl: This script shows how to draw a basic stacked bar chart, similar to that in MS Excel. It uses dummy data, so you can run it as-is. The script was contributed by Rashed Mahmood at CIIT Islamabad.
bar_17.ncl: This script shows how to customize a bar chart, using calls to to gsn_add_polygon and gsn_add_polyline.

In this case, the bars are filled one color to the right of an X reference line, and a different color to the left.

You can customize the colors, line thicknesses, etc, by modifying the code in the "customize_bar_chart" procedure in this script.

bar_18.ncl: This script shows how to draw a stacked bar chart with labelbars. It uses dummy data, so you can run it as-is. The script was contributed by Rashed Mahmood at CIIT Islamabad.
bar_19.ncl: This script shows how to force the bars in a series of plots to be the same width.

By default, the width of the bars is based on the width of the plot divided by the number of bars. This means if one plot has 10 bars and another plot has 5 bars, and the plots are the same width, then the bars in the 10-bar plot will be half the width of the bars in the 5-bar plot. To force the bars to be the same width, you need to scale the larger bars using the gsnXYBarChartBarWidth resource.

lb_22.ncl: This example shows how to apply different opacities to a bar plot and a custom labelbar.

The first frame is the plot without any opacity. The second frame has the opacities applied to both the filled bars and labelbar, using an RGBA array of colors with the opacity index (index 3) set to a range of opacities.

The third frame uses a resource introduced in NCL V6.4.0, lbFillOpacityF, that allows you to set a single opacity value for a labelbar. This resource is primarily meant to be used when creating a custom labelbar, and not with a labelbar associated with a filled contour or vector plot. As of NCL V6.4.0, any opacity applied to a contour or vector plot via resources like cnFillOpacityF or vcGlyphOpacityF will have the same opacity applied to the labelbar.

tm_3.ncl: Shows how to remove axis borders, tick marks, and tick mark labels. Demonstrates how to adjust the font height of tick mark labels.

(Top Panel) By setting tmYROn to False and tmYRBorderOn to False, the right Y-axis is completely turned off. (The right Y-axis and top X-axis labels are by default turned off.) The same is done to the top X-axis, using tmXTOn and tmXTBorderOn.

(Bottom Panel) tmXBLabelFontHeightF is used to set the bottom X-axis tick mark label font size, and tmYLLabelsOn is used to remove the left Y-axis tick mark labels.