The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


GD::Graph::histogram - Histogram plotting module for Perl5


  use GD::Graph::histogram;


GD::Graph::histogram extends the GD::Graph module to create histograms. The module allow creation of count or percentage histograms.


Fill an array with all the data values that are to be plotted. Note that GD::Graph::histogram unlike the other GD::Graph modules can only plot one data set at a time.

        $data = [1,5,7,8,9,10,11,3,3,5,5,5,7,2,2];

Create the graph

        my $graph = new GD::Graph::histogram(400,600);

Set graph options

                x_label         => 'X Label',
                y_label         => 'Count',
                title           => 'A Simple Count Histogram Chart',
                x_labels_vertical => 1,
                bar_spacing     => 0,
                shadow_depth    => 1,
                shadowclr       => 'dred',
                transparent     => 0,
        or warn $graph->error;

plot the graph

        my $gd = $graph->plot($data) or die $graph->error;

save the graph to a file

        open(IMG, '>histogram.png') or die $!;
        binmode IMG;
        print IMG $gd->png;


GD::Graph::histogram supports all the methods support by GD::Graph. Please refer to the GD::Graph documentation for more information.

The only method that behaves differently is plot

The plot method provided by GD::Graph::histogram expects a reference to an array of numbers.

Based on the input data, GD::Graph::histogram will generate the appropriate labels for the X axis. The X axis label represent the center point of the range of each histogram bin.


GD::Graph::histogram supports all the options supported by GD::Graph::bars. Please refer to the GD::Graph documentation for more information.

The two additional options that are specific to GD::Graph::histogram are:

                Specify the number of histogram bins to bucket the data into.
                The default is for the module to automatically computed the
                histogram bins based on the data.

                Can be set to either 'percentage' or 'count'. By default the module
                will create 'count' histograms.


As with all Modules for Perl: Please stick to using the interface. If you try to fiddle too much with knowledge of the internals of this module, you could get burned. I may change them at any time.


        Snehanshu Shah


Thanks for all the feedback, bug reports and bug fixes

Martin Corley Jonathan Barber William Miller


This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.


perl(1), GD::Graph