@@ -9,7 +9,7 @@ =head1 DESCRIPTION
99This is based on L<plots.pl> which will draw in either C<TikZ > or C<JSXGraph > format with the
1010default for the former to be used for hardcopy and the latter for HTML output.
1111
12- The statistical plot available are
12+ The statistical plots available are
1313
1414=over
1515
@@ -73,10 +73,16 @@ =head2 BAR PLOTS
7373ARRAY of heights of the bars. Note: if the option C<< orientation => 'horizontal' >> is included
7474then the bar lengths are the values in C<$xdata > and locations in C<$ydata > .
7575
76+ =head3 OPTIONS
77+
7678The options for the C<add_barplot > method are two fold. The following are specific to changing
7779the barplot, and the rest are passed along to C<add_rectangle > , which is a wrapper function for
7880C<add_dataset > .
7981
82+ =over
83+
84+ =item orientation
85+
8086The C<orientation > option can take on C<vertical > (default) or C<horizontal > to make vertical
8187or horizontal bars. Above was an example with vertical bars and an example with horizontal bars is
8288
@@ -89,21 +95,25 @@ =head2 BAR PLOTS
8995 bar_width => 0.9
9096 );
9197
98+ =item bar_width
99+
92100The option C<bar_width > is a number in the range [0,1] to give the relative width of the bar. If
93101C<< bar_width => 1 >> (default), then there is no gap between bars. In the example above, with
94102C<< bar_width => 0.9 >>, there is a small gap between bars.
95103
104+ =back
105+
96106Any remaining options are passed to C<add_rectangle > which has the same options as C<add_dataset > ,
97107however, if C<fill_color > is passed to C<add_barplot > , then the C<< fill => 'self' >> is also
98108passed along.
99109
100- See L<Options for add_dataset|plots.pl/DATASET OPTIONS> for specifics about other options to both changing fill and stroke
101- color.
110+ See L<Options for add_dataset|plots.pl/DATASET OPTIONS> for specifics about other options to
111+ both changing fill and stroke color.
102112
103113=head2 HISTOGRAMS
104114
105- A L<histogram|https://en.wikipedia.org/wiki/Histogram> is added with the `add_histogram` method to a C< StatPlot > . The general form
106- is
115+ A L<histogram|https://en.wikipedia.org/wiki/Histogram> is added with the `add_histogram` method
116+ to a C< StatPlot > . The general form is
107117
108118 $stat_plot->add_histogram($data, %options);
109119
@@ -167,7 +177,8 @@ =head3 Options
167177
168178=head2 BOX PLOTS
169179
170- A box plot (also called a box and whiskers plot) can be created with the C<add_boxplot > method. If one performs
180+ A box plot (also called a box and whiskers plot) can be created with the C<add_boxplot > method.
181+ If one performs
171182
172183 $stat_plot->add_boxplot($data, %options);
173184
@@ -201,8 +212,8 @@ =head2 BOX PLOTS
201212box plot (like fill color or stroke color and width) within the C<add_boxplot > method.
202213
203214If C<$data > is a hashref, it must contains the fields C<min, q1, median, q3, max > that are used to
204- define the boxplot. Optionally, one may also include the field C<outliers > which is an array ref of values
205- which will be plotted beyond the whiskers.
215+ define the boxplot. Optionally, one may also include the field C<outliers > which is an array
216+ ref of values which will be plotted beyond the whiskers.
206217
207218An example of this is
208219
@@ -289,7 +300,7 @@ =head2 SCATTER PLOTS
289300
290301 $stat_plot->add_scatterplot($data, marks => 'diamond', mark_size => 5, color => 'orange');
291302
292- This method is simply a wrapper for the C<add_dataset > method where the defaults are different. Specifically
303+ This method is simply a wrapper for the C<add_dataset > method where the defaults are different.
293304
294305=over
295306
@@ -299,7 +310,8 @@ =head2 SCATTER PLOTS
299310
300311=item marks
301312
302- The C<marks > is default to 'circle'. See L<plots.pl> for other mark options.
313+ The C<marks > is default to 'circle'. See L<Options for add_dataset|plots.pl/DATASET OPTIONS>
314+ for other mark options.
303315
304316=item mark_size
305317
@@ -324,36 +336,36 @@ sub _StatisticalPlots_init {
324336package Plots::StatPlot ;
325337our @ISA = qw( Plots::Plot) ;
326338
327- sub new {
328- my $self = shift ;
329- my $class = ref ($self ) || $self ;
330-
331- return $class -> SUPER::new(@_ );
332- }
333-
334339sub add_histogram {
335340 my ($self , $data , %opts ) = @_ ;
336341
337342 my %options = (
338- bins => 10,
343+ bins => 10,
344+ normalize => 0,
345+ orientation => ' vertical' ,
339346 %opts
340347 );
341348
342349 Value::Error(" The option 'bins' must be a positive integer" )
343350 unless $options {bins } =~ / ^\d +$ / && $options {bins } > 0;
344351
345- # if the bin_width is 0, set the num_bins to 1 and give a non-zero bin_width.
346-
347- my @counts ;
352+ my @counts = (0) x $options {bins };
348353 my $min = $options {min } // main::min(@$data );
349354 my $max = $options {max } // main::max(@$data );
350355 my $bin_width = ($max - $min ) / $options {bins };
351356
357+ # TODO: if the bin_width is 0, set the num_bins to 1 and give a non-zero bin_width.
358+
352359 $counts [ int (($_ - $min ) / $bin_width ) ]++ for (@$data );
360+ if ($options {normalize }) {
361+ my $total = 0;
362+ $total += $_ for (@counts );
363+ @counts = map { $_ / $total } @counts ;
364+ }
353365 my @xdata = map { $min + (0.5 + $_ ) * $bin_width } (0 .. $#counts );
354366
355367 # Remove these options and pass the rest to add_barplot
356- delete $options {$_ } for (' min' , ' max' , ' bins' );
368+ delete $options {$_ } for (' min' , ' max' , ' bins' , ' normalize ' );
357369
358370 if ($options {orientation } eq ' vertical' ) {
359371 $self -> add_barplot(\@xdata , \@counts , %options );
0 commit comments