source: trunk/SRC/ToBeReviewed/PLOTS/DESSINE/bar_plot.pro @ 163

Last change on this file since 163 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.3 KB
Line 
1;+
2;
3; @file_comments
4; Create a bar graph, or overplot on an existing one.
5;
6; @categories
7; Graphics
8;
9; @param Values {in}{required} {type=vector}
10; A vector containing the values to be represented by the bars.
11; Each element in VALUES corresponds to a single bar in the
12; output.
13;
14; @keyword BASELINES {type=vector}
15; A vector, the same size as VALUES, that contains the
16; base value associated with each bar.  If not specified,
17; a base value of zero is used for all bars.
18;
19; @keyword COLORS {type=vector}
20; A vector, the same size as VALUES, containing the color index
21; to be used for each bar.  If not specified, the colors are
22; selected based on spacing the color indexes as widely as
23; possible within the available colors (specified by D.N_COLORS).
24;
25; @keyword BARNAMES {type=string}
26; A string array, containing one string label per bar.
27; If the bars are vertical, the labels are placed beneath
28; them.  If horizontal (rotated) bars are specified, the labels
29; are placed to the left of the bars.
30;
31; @keyword TITLE {type=string}
32; A string containing the main title to for the bar plot.
33;
34; @keyword XTITLE {type=string}
35; A string containing the title for the X axis.
36;
37; @keyword YTITLE {type=string}
38; A string containing the title for the Y axis.
39;
40; @keyword BASERANGE {type=float}
41; A floating-point scalar in the range 0.0 to 1.0, that
42; determines the fraction of the total available plotting area
43; (in the direction perpendicular to the bars) to be used.
44; If not specified, the full available area is used.
45;
46; @keyword BARWIDTH {type=float}
47; A floating-point value that specifies the width of the bars
48; in units of "nominal bar width".  The nominal bar width is
49; computed so that all the bars (and the space between them,
50; set by default to 20% of the width of the bars) will fill the
51; available space (optionally controlled with the BASERANGE
52; keyword).
53;
54; @keyword BARSPACE {type=scalar}
55; A scalar that specifies, in units of "nominal bar width",
56; the spacing between bars.  For example, if BARSPACE is 1.0,
57; then all bars will have one bar-width of space between them.
58; If not specified, the bars are spaced apart by 20% of the bar
59; width.
60;
61; @keyword BAROFFSET {default=BARSPACE}{type=scalar}
62; A scalar that specifies the offset to be applied to the
63; first bar, in units of "nominal bar width".  This keyword
64; allows, for example, different groups of bars to be overplotted
65; on the same graph.
66;
67; @keyword OUTLINE
68; If set, this keyword specifies that an outline should be
69; drawn around each bar.
70;
71; @keyword OVERPLOT
72; If set, this keyword specifies that the bar plot should be
73; overplotted on an existing graph.
74;
75; @keyword BACKGROUND {default=The normal IDL background color is used}{type=scalar}
76; A scalar that specifies the color index to be used for
77; the background color.
78;
79; @keyword ROTATE
80; If set, this keyword indicates that horizontal rather than
81; vertical bars should be drawn.  The bases of horizontal bars
82; are on the left, "Y" axis and the bars extend to the right.
83;
84; @examples By using the overplotting capability, it is relatively easy to create
85;       stacked bar charts, or different groups of bars on the same graph.
86;
87;       For example, if ARRAY is a two-dimensional array of 5 columns and 8
88;       rows, it is natural to make a plot with 5 bars, each of which is a
89;       "stacked" composite of 8 sections.  First, create a 2D COLORS array,
90;       equal in size to ARRAY, that has identical color index values across
91;       each row to ensure that the same item is represented by the same color
92;       in all bars.
93;
94;       With ARRAYS and COLORS defined, the following code fragment
95;       illustrates the creation of stacked bars (note that the number of rows
96;       and columns is arbitrary):
97;
98;       !Y.RANGE = [0,ymax] ; Scale range to accommodate the total bar lengths.
99;       BASE = INTARR(NROWS)
100;       FOR I = 0, NROWS-1 DO BEGIN
101;          BAR_PLOT, ARRAY(*,I), COLORS=COLORS(*,I), BASELINES=BASE, $
102;                    BARWIDTH=0.75, BARSPACE=0.25, OVER=(I GT 0)
103;          BASE = BASE + ARRAY(*,I)
104;       ENDFOR
105;
106;       To plot each row of ARRAY as a clustered group of bars within the same
107;       graph, use the BASERANGE keyword to restrict the available plotting
108;       region for each set of bars.  The sample code fragment below
109;       illustrates this method:
110;
111;       FOR I = 0, NROWS-1 DO $
112;          BAR_PLOT, ARRAY(*,I), COLORS=COLORVECT, BARWIDTH=0.8,BARSPACE=0.2, $
113;            BAROFFSET=I*((1.0+BARSPACE)*NCOLS), OVER=(I GT 0), BASERANGE=0.19
114;
115;       where NCOLS is the number of columns in ARRAY, and COLORVECT is a
116;       vector containing the color indices to be used for each group of
117;       bars.  (In this example, each group uses the same set of colors, but
118;       this could easily be changed.)
119;
120; @history August 1990, T.J. Armitage, RSI, initial programming.  Replacement
121;       for PLOTBAR and OPLOTBAR routines written by William Thompson.
122;
123;       September 1990, Steve Richards, RSI, changed defaults to improve the
124;       appearance of the bar plots in the default mode. Included
125;       spacing the bars slightly.
126;
127; $Id$
128;
129;-
130pro bar_plot,values,baselines=baselines,colors=colors,barnames=barnames, $
131          title=title,xtitle=xtitle,ytitle=ytitle,baserange=baserange, $
132          barwidth=barwidth,barspace=barspace,baroffset=baroffset, $
133          outline=outline,overplot=overplot,background=background, $
134          rotate=rotate, _EXTRA = ex
135;
136  compile_opt idl2, strictarrsubs
137;
138if (n_params(d) eq 0) then begin  ;Print call & return if no parameters
139  print,'bar_test,values,baselines=baselines,colors=colors,barnames=barnames,$'
140  print,' title=title,xtitle=xtitle,ytitle=ytitle,baserange=baserange, $'
141  print,' barwidth=barwidth,barspace=barspace,baroffset=baroffset, $'
142  print,' outline=outline,overplot=overplot,background=background, $'
143  print,' rotate=rotate'
144  return
145endif
146
147nbars=n_elements(values)                ; Determine number of bars
148; Baselines (bars extend from baselines through values); default=0
149if not(keyword_set(baselines)) then baselines=intarr(nbars)
150; Default colors spaced evenly in current color table
151if not(keyword_set(colors)) then $
152   colors=fix((!d.n_colors/float(nbars))*(indgen(nbars)+0.5))
153; Labels for the individual bars; none by default
154if not(keyword_set(barnames)) then barnames=strarr(nbars)+' '
155; Main title
156if not(keyword_set(title)) then title=''
157; Centered title under X-axis
158if not(keyword_set(xtitle)) then xtitle=''
159; Title for Y-axis
160if not(keyword_set(ytitle)) then ytitle=''
161; Fraction (0-1) of full X range to use
162if not(keyword_set(baserange)) then baserange=1.0
163; Space betw. bars, taken from nominal bar widths; default is none
164If not(keyword_set(barspace)) then barspace=0.2
165; Bar width scaling factor, relative to nominal
166if not(keyword_set(barwidth)) then barwidth=1.0 - barspace - barspace / nbars
167; Initial X offset, in scaled bar widths; default is none
168if not(keyword_set(baroffset)) then baroffset=barspace/barwidth
169; Outline of bars; default is none
170outline = keyword_set(outline)
171; Overplot (do not erase the existing display); default is to create new plot
172overplot = keyword_set(overplot)
173; Background color index; defaults to 0 (usually black) if not specified
174if not(keyword_set(background)) then background=0
175; Rotate (make horizontal bars); default is vertical bars
176rotate = keyword_set(rotate)
177
178mnB = MIN(baselines, MAX=mxB, /NAN)
179mnV = MIN(values, MAX=mxV, /NAN)
180range=[mnB < mnV, $    ;Minimum of bases & values
181                mxB > mxV]      ;Maximum of bases & values
182
183if (rotate) then begin                             ;Horizontal bars
184   if (!x.range[0] eq 0) and (!x.range[1] eq 0) $  ;Determine range for X-axis
185      then xrange=range $
186      else xrange=!x.range                         ;Or, use range specified
187   if (!y.range[0] eq 0) and (!y.range[1] eq 0) $  ;Plot will calculate
188      then $                                       ; defaults for X, but not
189        yrange = [0, n_elements(values)] $         ; for Ys, so fill in here.
190      else $
191        yrange=!y.range                            ;Axis perpend. to bars
192   yticks=1                                        ;Suppress ticks in plot
193   ytickname=strarr(2)+' '
194   xticks=0
195   xtickname=strarr(1)+''
196endif else begin                                   ;Vertical bars
197   if (!y.range[0] eq 0) and (!y.range[1] eq 0) $  ;Determine range for Y-axis
198      then yrange=range $
199      else yrange=!y.range                         ;Or, use range specified
200   xrange=!x.range                                 ;Axis perpend. to bars
201   xticks=1                                        ;Suppress ticks in plot
202   xtickname=strarr(2)+' '
203   yticks=0
204   ytickname=strarr(1)+''
205endelse
206if (overplot eq 0) then $                          ;Create new plot, no data
207plot,[values],/nodata,title=title,xtitle=xtitle,ytitle=ytitle, $
208   noerase=overplot,xrange=xrange,yrange=yrange,xticks=xticks, $
209   xtickname=xtickname,yticks=yticks,ytickname=ytickname, $
210   xstyle=1,ystyle=1,/data,background=background, _EXTRA = ex
211if (rotate) then begin                             ;Horizontal bars
212   base_win=!y.window                              ;Window range in Y
213   scal_fact=!x.s                                  ;Scaling factors
214   tick_scal_fact=!y.s                             ;Tick scaling factors
215endif else begin                                   ;Vertical bars
216   base_win=!x.window                              ;Window range in X
217   scal_fact=!y.s                                  ;Scaling factors
218   tick_scal_fact=!x.s                             ;Tick scaling factors
219endelse
220winrange=baserange*(base_win[1]-base_win[0])       ;Normal. window range
221barsize=barwidth*winrange/nbars                    ;Normal. bar width
222winoffset=base_win[0]+(baroffset*barsize)          ;Normal. first offset
223bases=scal_fact[0]+(scal_fact[1]*baselines)        ;Baselines, in normal coor.
224normal=scal_fact[0]+(scal_fact[1]*values)          ;Values, in normal coor.
225barstart=indgen(nbars)*(barsize+barspace*(winrange/nbars)) ;Coor. at left edges
226tickv=winoffset+barstart+(0.5*barsize)             ;Tick coor. (centered)
227for i=0,nbars-1 do begin                           ;Draw the bars
228   width=winoffset+[barstart[i],barstart[i], $     ;Compute bar width
229     (barstart[i]+barsize),(barstart[i]+barsize)]
230   length=[bases[i],normal[i],normal[i],bases[i]]  ;Compute bar length
231   if (rotate) then begin                          ;Horizontal bars
232      x=length                                     ;X-axis is "length" axis
233      y=width                                      ;Y-axis is "width" axis
234   endif else begin                                ;Vertical bars
235      x=width                                      ;X-axis is "width" axis
236      y=length                                     ;Y-axis is "length" axis
237   endelse
238   polyfill,x,y,color=colors[i],/normal            ;Polyfill with color
239   if (outline) then plots,x,y,/normal             ;Outline using !p.color
240endfor
241
242tickv=(tickv-tick_scal_fact[0])/tick_scal_fact[1]  ;Locations of the ticks
243if (rotate) then $                                 ;Label the bars (Y-axis)
244  axis,yaxis=0,ystyle=1,yticks=(nbars-1),ytickv=tickv,ytickname=barnames, $
245  yticklen=0.0 $
246else $                                             ;Label the bars (X-axis)
247  axis,xaxis=0,xstyle=1,xticks=(nbars-1),xtickv=tickv,xtickname=barnames, $
248  xticklen=0.0
249return
250end
Note: See TracBrowser for help on using the repository browser.