Sarat1729 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Can someone please explain the usage of use_axis which will be used in plotting graphs in GD::Graph. I will be obliged if it can be explained with the help of an example containing 3 or more datasets.

Replies are listed 'Best First'.
Re: Usage of use_axis
by pryrt (Abbot) on Jun 06, 2018 at 12:59 UTC

    What did you not understand from Re^3: Plotting the data using different axes and the other responses in that other thread you started, where the answer to this question was covered in detail, with coding examples?

    Update 1: Re-reading your follow on Re^2: Usage of use_axis, I see that you say "Even though, I mention 2 in use_axis to force one of the datasets to refer to the right Y axis, it is not working when the datasets are more than 3." I will work on an example with multiple (>3) datasets, with two significantly different output ranges, so it's obvious which scale each is graphed on, and will post it as another edit in a few minutes.

    Update 2: sorry for the delay. This version of the code

    #!/usr/bin/perl use warnings; use strict; use GD::Graph::mixed; my @data = ( ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"], [ 1, 4, 9, 16, 25, 36, 49, 64, 81], # +wants left: 0 .. 100 'x**2', [ 1, 1.4, 1.7, 2, 2.2, 2.4, 2.6, 2.8, 3], # +wants right: 0 .. 5 '~sqrt(x)' [ 99, 88, 77, 66, 55, 44, 33, 22, 11], # +wants left: 0 .. 100 '11*(10-x)', [ 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5], # +wants right: 0 .. 5 '0.5x', [ 1, 2, 3, 4, 5, 4, 3, 2, 1], # +wants right: 0 .. 5 '123454321' [ 98, 76, 54, 32, 10, '', '', '', ''], # +wants left: 0 .. 100 'descend', ); my $my_graph = new GD::Graph::mixed( ); my %opts = ( transparent => '0', x_label => 'nth', y1_label => 'big', y2_label => 'small', title => 'Using two axes', y1_max_value => 100, y2_max_value => 5, y_tick_number => 8, y_label_skip => 2, long_ticks => 1, use_axis => [1,2,1,2,2,1], # big, small, big, small, small, b +ig legend_placement => 'BR', x_labels_vertical => 1, x_label_position => 1/2, two_axes => 1, legend_marker_width => 100, l_margin => 10, b_margin => 10, r_margin => 20, t_margin => 10, show_values => 1, ); $my_graph->set(%opts); $my_graph->set_legend( 'x**2', '~sqrt(x)', '11*(10-x)', '0.5x', '12345 +4321', 'descend'); save_it($my_graph, \@data, 'out1.png'); # second graph, same as the first (except for use_axis) $opts{use_axis} = [1,2,1,1,1,1]; # big, small, big, big +<will shrink>, big<will shrink> my $second_graph = new GD::Graph::mixed( ); $second_graph->set(%opts); $second_graph->set_legend( 'x**2', '~sqrt(x)', '11*(10-x)', '0.5x', '1 +23454321', 'descend'); save_it($second_graph, \@data, 'out2.png'); sub save_it { my ($graph, $data_ref, $fname) = @_; my $gd = $graph->plot($data_ref) or die $graph->error; open(my $img, '>', $fname) or die "$fname: $!\n"; binmode $img; print {$img} $gd->png; close $img; }

    Running that code, do you see how '123454321' and '0.5x' take up the bulk of the vertical height in "out1.png" and are down near the x-axis in "out2.png". That because they've properly switched from axis2 in "out1" to axis1 in "out2".

    Update 3: further, if you were to change to $opts{use_axis} = [1,2,2,2,2,2];, which puts some of the "big" datasets on the "small" y axis, you will get an error ("Maximum for y3 too small at ..."), which shows that the third data set '11*(10-x)' tried to go on the small axis, where it doesn't fit.

      ... Further: if your difficulty is that you were expecting the GD::Graph::multi to automatically disambiguate the two axis: that's not going to happen. But you can do it yourself. For example, in the modified version of the code below, I use the elements of the $opts{use_axis} array to set the value of $opts{types} to 'lines' (for the left axis) or 'points' (for the right axis).

      #!/usr/bin/perl use warnings; use strict; use GD::Graph::mixed; my @data = ( ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"], [ 1, 4, 9, 16, 25, 36, 49, 64, 81], # +wants left: 0 .. 100 'x**2', [ 1, 1.4, 1.7, 2, 2.2, 2.4, 2.6, 2.8, 3], # +wants right: 0 .. 5 '~sqrt(x)' [ 99, 88, 77, 66, 55, 44, 33, 22, 11], # +wants left: 0 .. 100 '11*(10-x)', [ 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5], # +wants right: 0 .. 5 '0.5x', [ 1, 2, 3, 4, 5, 4, 3, 2, 1], # +wants right: 0 .. 5 '123454321' [ 98, 76, 54, 32, 10, '', '', '', ''], # +wants left: 0 .. 100 'descend', ); my $my_graph = new GD::Graph::mixed( ); my %opts = ( transparent => '0', x_label => 'nth', y1_label => 'big', y2_label => 'small', title => 'Using two axes', y1_max_value => 100, y2_max_value => 5, y_tick_number => 8, y_label_skip => 2, long_ticks => 1, use_axis => [1,2,1,2,2,1], # big, small, big, small, small, b +ig legend_placement => 'BR', x_labels_vertical => 1, x_label_position => 1/2, two_axes => 1, legend_marker_width => 100, l_margin => 10, b_margin => 10, r_margin => 20, t_margin => 10, show_values => 1, ); $opts{types} = [ (undef, 'lines', 'points')[@{$opts{use_axis}}] ]; # +if use_axis[n]==0, don't graph; if use_axis[n]==1, graph with lines; +if use_axis[n]==2, graph with points $my_graph->set(%opts); $my_graph->set_legend( 'x**2', '~sqrt(x)', '11*(10-x)', '0.5x', '12345 +4321', 'descend'); save_it($my_graph, \@data, 'out1.png'); # second graph, same as the first (except for use_axis) $opts{use_axis} = [1,2,1,1,1,1]; # big, small, big, big +<will shrink>, big<will shrink>, big $opts{types} = [ (undef, 'lines', 'points')[@{$opts{use_axis}}] ]; my $second_graph = new GD::Graph::mixed( ); $second_graph->set(%opts); $second_graph->set_legend( 'x**2', '~sqrt(x)', '11*(10-x)', '0.5x', '1 +23454321', 'descend'); save_it($second_graph, \@data, 'out2.png'); sub save_it { my ($graph, $data_ref, $fname) = @_; my $gd = $graph->plot($data_ref) or die $graph->error; open(my $img, '>', $fname) or die "$fname: $!\n"; binmode $img; print {$img} $gd->png; close $img; }

      update: added images to imgur: out1.png, out2.png

Re: Usage of use_axis
by hippo (Archbishop) on Jun 06, 2018 at 07:37 UTC
      Exactly the same. But what I would like to understand is how the datasets are related to use_axis. Even though, I mention 2 in use_axis to force one of the datasets to refer to the right Y axis, it is not working when the datasets are more than 3.
        I'm not sure if GD::Graph::mixed will make it easy to do that in the way you want. You may need to use a more powerful graphics module, like GnuPlot; OR you may be able to combine separate GD images into one composite which would do what you want. One image would be the multiple plots, and the second would be a set of multiple y-axis values, color coded to match the data. See multi-y-axis plot. See multiple y plots for more information.

        You can do anything with Perl. If you are willing to manually make your axis and plots, you can do it on a Tk::Canvas, or use something direct like Cairo


        I'm not really a human, but I play one on earth. ..... an animated JAPH