Skip to content

Commit c361bca

Browse files
committed
Add normalize functionality to add_histogram.
Cleanup of the POD. Removal of Plots::StatsPlot->new function. It wasn't needed. Make sure Plots::StatsPlot objects are rendered in PGbasicmacros.pl.
1 parent 27ce50e commit c361bca

5 files changed

Lines changed: 37 additions & 68 deletions

File tree

lib/Plots/Plot.pm

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,7 @@ sub add_rectangle {
399399
unless ref($pt0) eq 'ARRAY' && scalar(@$pt0) == 2;
400400
Value::Error('The second point must be an array ref of length 2')
401401
unless ref($pt2) eq 'ARRAY' && scalar(@$pt2) == 2;
402-
my $pt1 = [ $pt2->[0], $pt0->[1] ];
403-
my $pt3 = [ $pt0->[0], $pt2->[1] ];
404-
return $self->add_dataset($pt0, $pt1, $pt2, $pt3, $pt0, %options);
402+
return $self->add_dataset($pt0, [ $pt2->[0], $pt0->[1] ], $pt2, [ $pt0->[0], $pt2->[1] ], $pt0, %options);
405403
}
406404

407405
sub add_vectorfield {

lib/Plots/StatPlot.pm

Lines changed: 0 additions & 38 deletions
This file was deleted.

macros/core/PGbasicmacros.pl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,10 +2942,7 @@ sub image {
29422942
$width_ratio = 0.001 * $image_item->{tex_size};
29432943
}
29442944
$image_item = insertGraph($image_item)
2945-
if (ref $image_item eq 'WWPlot'
2946-
|| ref $image_item eq 'Plots::Plot'
2947-
|| ref $image_item eq 'PGlateximage'
2948-
|| ref $image_item eq 'PGtikz');
2945+
if (grep { $image_item eq $_ } ('WWPlot', 'Plots::Plot', 'Plots::StatPlot', 'PGlateximage', 'PGtikz'));
29492946
my $imageURL = alias($image_item) // '';
29502947
$imageURL = ($envir{use_site_prefix}) ? $envir{use_site_prefix} . $imageURL : $imageURL;
29512948
my $id = $main::PG->getUniqueName('img');

macros/graph/StatisticalPlots.pl

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ =head1 DESCRIPTION
99
This is based on L<plots.pl> which will draw in either C<TikZ> or C<JSXGraph> format with the
1010
default 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
7373
ARRAY of heights of the bars. Note: if the option C<< orientation => 'horizontal' >> is included
7474
then the bar lengths are the values in C<$xdata> and locations in C<$ydata>.
7575
76+
=head3 OPTIONS
77+
7678
The options for the C<add_barplot> method are two fold. The following are specific to changing
7779
the barplot, and the rest are passed along to C<add_rectangle>, which is a wrapper function for
7880
C<add_dataset>.
7981
82+
=over
83+
84+
=item orientation
85+
8086
The C<orientation> option can take on C<vertical> (default) or C<horizontal> to make vertical
8187
or 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+
92100
The option C<bar_width> is a number in the range [0,1] to give the relative width of the bar. If
93101
C<< bar_width => 1 >> (default), then there is no gap between bars. In the example above, with
94102
C<< bar_width => 0.9 >>, there is a small gap between bars.
95103
104+
=back
105+
96106
Any remaining options are passed to C<add_rectangle> which has the same options as C<add_dataset>,
97107
however, if C<fill_color> is passed to C<add_barplot>, then the C<< fill => 'self' >> is also
98108
passed 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
201212
box plot (like fill color or stroke color and width) within the C<add_boxplot> method.
202213
203214
If 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
207218
An 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 {
324336
package Plots::StatPlot;
325337
our @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-
334339
sub 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);

macros/graph/plots.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ =head1 NAME
55
66
=head1 DESCRIPTION
77
8-
This macro creates a Plots object that is used to add data of different
8+
This macro creates a Plot object that is used to add data of different
99
elements of a 2D plot, then draw the plot. The plots can be drawn using different
1010
formats. Currently C<TikZ> (using PGFplots) and C<JSXGraph> graphics format are
1111
available. The default is to use C<JSXGraph> for HTML output and C<TikZ> for

0 commit comments

Comments
 (0)