1 //      Box plot demo.
2 //
3 // Copyright (C) 2008 by FLLL <http://www.flll.jku.at>
4 // Author: Robert Pollak <robert.pollak@jku.at>
5 // Copyright (C) 2009 Andrew Ross
6 //
7 // This file is part of PLplot.
8 //
9 //  PLplot is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU Library General Public License as published
11 // by the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // PLplot is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU Library General Public License for more details.
18 //
19 // You should have received a copy of the GNU Library General Public License
20 // along with PLplot; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 //
24 
25 #include "plcdemos.h"
26 
27 void
28 plfbox( PLFLT x, PLFLT y25, PLFLT y50, PLFLT y75, PLFLT lw, PLFLT uw );
29 void
30 plcircle( PLFLT x, PLFLT y );
31 
32 //--------------------------------------------------------------------------
33 // main
34 //
35 // Does a box plot.
36 //--------------------------------------------------------------------------
37 
38 int
main(int argc,char * argv[])39 main( int argc, char *argv[] )
40 {
41     int          i;
42     char         string[20];
43     PLFLT        x0;
44     // quartiles
45     static PLFLT y25[] = { 0.984, 0.980, 0.976, 0.975, 0.973,
46                            0.967, 0.974, 0.954, 0.987, 0.991 };
47     static PLFLT y50[] = { 0.994, 0.999, 1.035, 0.995, 1.002,
48                            0.997, 1.034, 0.984, 1.007, 1.017 };
49     static PLFLT y75[] = { 1.054, 1.040, 1.066, 1.025, 1.043,
50                            1.017, 1.054, 1.004, 1.047, 1.031 };
51 
52     // lower and upper whisker
53     static PLFLT lw[] = { 0.964, 0.950, 0.926, 0.955, 0.963,
54                           0.937, 0.944, 0.924, 0.967, 0.941 };
55 
56     static PLFLT uw[] = { 1.071, 1.062, 1.093, 1.045, 1.072,
57                           1.067, 1.085, 1.024, 1.057, 1.071 };
58 
59     // outliers
60     static PLFLT outx[] = { 3.5, 6.5 };
61     static PLFLT outy[] = { 0.89, 1.09 };
62 //
63 //  static PLFLT pos[] = {0.0, 0.25, 0.5, 0.75, 1.0};
64 //  static PLFLT red[] = {0.0, 0.25, 0.5, 1.0, 1.0};
65 //  static PLFLT green[] = {1.0, 0.5, 0.5, 0.5, 1.0};
66 //  static PLFLT blue[] = {1.0, 1.0, 0.5, 0.25, 0.0};
67 //
68 // Parse and process command line arguments
69 
70     (void) plparseopts( &argc, argv, PL_PARSE_FULL );
71 
72 // Initialize plplot
73 
74     plinit();
75 
76     pladv( 0 );
77     plvsta();
78 
79     x0 = 1.;
80     plwind( x0, x0 + 10, 0.85, 1.15 );
81     plcol0( 1 );
82     plbox( "bc", 1.0, 0, "bcgnst", 0, 0 );
83     pllab( "Group", "Value", "#frPLplot Example 32" );
84 
85     // plscmap1l(1,5,pos,red,green,blue,NULL);
86 
87     for ( i = 0; i < 10; i++ )
88     {
89         plcol1( i / 9.0 );
90         plfbox( ( x0 + i ), y25[i], y50[i], y75[i], lw[i], uw[i] );
91 
92         sprintf( string, "%d", (int) x0 + i );
93         plmtex( "b", 1.0, ( ( i + 1 ) * .1 - .05 ), 0.5, string );
94     }
95 
96     // some outliers plotted with intermediate-sized circles, see PLplot
97     // example 06 for other possibilities.
98     plpoin( 2, outx, outy, 22 );
99 
100 // Don't forget to call plend() to finish off!
101 
102     plend();
103     exit( 0 );
104 }
105 
106 void
plfbox(PLFLT x,PLFLT y25,PLFLT y50,PLFLT y75,PLFLT lw,PLFLT uw)107 plfbox( PLFLT x, PLFLT y25, PLFLT y50, PLFLT y75, PLFLT lw, PLFLT uw )
108 {
109     PLFLT px[5], py[5], mx[2], my[2], wx[2], wy[2], barx[2], bary[2];
110     PLFLT spacing;
111     PLFLT xmin, xmax;
112     PLFLT xmid, xwidth;
113 
114     spacing = .4; // in x axis units
115 
116     xmin = x + spacing / 2.;
117     xmax = x + 1. - spacing / 2.;
118 
119     // box
120 
121     px[0] = xmin;
122     py[0] = y25;
123     px[1] = xmin;
124     py[1] = y75;
125     px[2] = xmax;
126     py[2] = y75;
127     px[3] = xmax;
128     py[3] = y25;
129     px[4] = xmin;
130     py[4] = y25;
131 
132     plpsty( 0 );
133     plfill( 4, px, py );
134     plcol0( 1 );
135     pllsty( 1 );
136     plline( 5, px, py );
137 
138 
139     // median
140 
141     mx[0] = xmin;
142     my[0] = y50;
143     mx[1] = xmax;
144     my[1] = y50;
145 
146     pllsty( 1 );
147     plline( 2, mx, my );
148 
149     // lower whisker
150 
151     xmid   = ( xmin + xmax ) / 2.;
152     xwidth = xmax - xmin;
153     wx[0]  = xmid;
154     wy[0]  = lw;
155     wx[1]  = xmid;
156     wy[1]  = y25;
157 
158     pllsty( 2 ); // short dashes and gaps
159     plline( 2, wx, wy );
160 
161     barx[0] = xmid - xwidth / 4.;
162     bary[0] = lw;
163     barx[1] = xmid + xwidth / 4.;
164     bary[1] = lw;
165 
166     pllsty( 1 );
167     plline( 2, barx, bary );
168 
169     // upper whisker
170 
171     xmid   = ( xmin + xmax ) / 2.;
172     xwidth = xmax - xmin;
173     wy[0]  = y75;
174     wy[1]  = uw;
175 
176     pllsty( 2 ); // short dashes and gaps
177     plline( 2, wx, wy );
178 
179     bary[0] = uw;
180     bary[1] = uw;
181 
182     pllsty( 1 );
183     plline( 2, barx, bary );
184 }
185