BAR_PLOT

The BAR_PLOT procedure creates a bar graph.

This routine is written in the IDL language. Its source code can be found in the file bar_plot.pro in the lib subdirectory of the IDL distribution.

Calling Sequence

BAR_PLOT, Values

Arguments

Values

A vector containing the values to be represented by the bars. Each element in Values corresponds to a single bar in the output.

Keywords

BACKGROUND

A scalar that specifies the color index to be used for the background color. By default, the normal IDL background color is used.

BARNAMES

A string array, containing one string label per bar. If the bars are vertical, the labels are placed beneath them. If horizontal (rotated) bars are specified, the labels are placed to the left of the bars.

BAROFFSET

A scalar that specifies the offset to be applied to the first bar, in units of "nominal bar width". This keyword allows, for example, different groups of bars to be overplotted on the same graph. If not specified, the default offset is equal to BARSPACE.

BARSPACE

A scalar that specifies, in units of "nominal bar width", the spacing between bars. For example, if BARSPACE is 1.0, then all bars will have one bar-width of space between them. If not specified, the bars are spaced apart by 20% of the bar width.

BARWIDTH

A floating-point value that specifies the width of the bars in units of "nominal bar width". The nominal bar width is computed so that all the bars (and the space between them, set by default to 20% of the width of the bars) will fill the available space (optionally controlled with the BASERANGE keyword).

BASELINES

A vector, the same size as Values , that contains the base value associated with each bar. If not specified, a base value of zero is used for all bars.

BASERANGE

A floating-point scalar in the range 0.0 to 1.0, that determines the fraction of the total available plotting area (in the direction perpendicular to the bars) to be used. If not specified, the full available area is used.

COLORS

A vector, the same size as Values , containing the color index to be used for each bar. If not specified, the colors are selected based on spacing the color indices as widely as possible within the range of available colors (specified by !D.N_COLORS).

OUTLINE

If set, this keyword specifies that an outline should be drawn around each bar.

OVERPLOT

If set, this keyword specifies that the bar plot should be overplotted on an existing graph.

ROTATE

If set, this keyword indicates that horizontal rather than vertical bars should be drawn. The bases of horizontal bars are on the left, "Y" axis and the bars extend to the right.

TITLE

A string containing the main title to for the bar plot.

XTITLE

A string containing the title for the X axis.

YTITLE

A string containing the title for the Y axis.

Example

By using the overplotting capability, it is relatively easy to create stacked bar charts, or different groups of bars on the same graph.

For example, if ARRAY is a two-dimensional array of 5 columns and 8 rows, it is natural to make a plot with 5 bars, each of which is a "stacked" composite of 8 sections. First, create a 2D COLORS array, equal in size to ARRAY, that has identical color index values across each row to ensure that the same item is represented by the same color in all bars.

array = INDGEN(5,8)

colors = INTARR(5,8)

FOR I = 0, 7 DO colors[*,I]=(20*I)+20

With ARRAYS and COLORS defined, the following code fragment illustrates the creation of stacked bars (note that the number of rows and columns is arbitrary):

!Y.RANGE = [0, MAX(array)] ; Scale range to accommodate the total bar lengths.

nrows = N_ELEMENTS(array[0,*])

base = INTARR(nrows)

FOR I = 0, nrows-1 DO BEGIN

  BAR_PLOT, array[*,I], COLORS=colors[*,I], BASELINES=base, $

    BARWIDTH=0.75, BARSPACE=0.25, OVER=(I GT 0)

  base = array[*,I]

ENDFOR

To plot each row of ARRAY as a clustered group of bars within the same graph, use the BASERANGE keyword to restrict the available plotting region for each set of bars. The sample code fragment below illustrates this method:

ncols = N_ELEMENTS(array[*,0])

FOR I = 0, nrows-1 DO $

  BAR_PLOT, array[*,I], COLORS=colors[*,I], BARWIDTH=0.8, $

    BARSPACE=0.2, BAROFFSET=I*(1.5*ncols), $

    OVER=(I GT 0), BASERANGE=0.19

where NCOLS is the number of columns in ARRAY. (In this example, each group uses the same set of colors, but this could easily be changed.)

See Also

PLOT , PSYM Graphics Keyword