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

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

english and nicer header (2a)

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