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

Dear Monks, How can I format 2 datasets (each with an array of x and y values) into a multi-dimensional array that GD::Graph::points can create a scatter graph from?? e.g.
#data-set 1 has @x_values1 and @y_values1 # data-set 2 has @x_values2 and @y_values2 # for one data-set it is: my @graph_data = (\@x_values1, \@y_values1);
I have seen many examples where this is done for a small no of points (where this can easily be types in), but am struggling to do this from arrays. Hope you can help!

Replies are listed 'Best First'.
Re: GD::Graph::points + multiple data sets
by Melly (Chaplain) on Oct 08, 2003 at 13:23 UTC

    I may be missing the obvious here, but can't you concatenate the two arrays together?

    @x_values_all = (@x_values1, @x_values2); @y_values_all = (@y_values1, @y_values2);
    Tom Melly, tom@tomandlu.co.uk
      you can do this for the x-axis but not the y-axis too as then you will have only one data-set! I have tried the following, but it seems to miss out some data-points (not sure why).
      my @totalx_vals = (@x_vals1, @x_vals2); my @graph_data = (\@totalx_vals, \@y_vals1, \@y_vals2);

        I'm confused - you did try the logical thing of doing this twice - once for the 2 sets of x-vals and once for the 2 sets of y-vals?

        I haven't yet used GD::Graph, but the syntax you gave seems to imply that

        my @graph_data = (\@x_vals, \@y_vals)
        is the expected format. So...
        @x_vals = (@x_vals1, @x_vals2); @y_vals = (@y_vals1, @y_vals2); my @graph_data = (\@x_vals, \@y_vals);

        If this isn't the solution, then you'll have to wait for someone who knows perl and/or GD::Graph better than me...

        Tom Melly, tom@tomandlu.co.uk
Re: GD::Graph::points + multiple data sets
by CombatSquirrel (Hermit) on Oct 08, 2003 at 15:37 UTC
    If you want to create a graph incorporating independent data sets, you might want to have a look at GD::Graph::mixed. I have not worked with it, but it seems to be what you are looking for.
    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: GD::Graph::points + multiple data sets
by kryberg (Pilgrim) on Oct 08, 2003 at 20:01 UTC
    I've plotted data sets with different y values, but they used the same array of x values. I'm not sure that you can use two sets of x values.

    I have the O'Reilly book Programming Web Graphics with Perl & GNU Software. It is rather out of date because the graphing section it uses GIFgraph, which is deprecated, but there are few differences between GIFgraph and GD::Graph.

    Here's what the book says for datasets:
    "The data for the graph must be in a very particular form before you plot the graph. It must be assigned to an array, where the first element of the array is an anonymous list of the x axis values, and any subsequent elements are different data sets to be plotted against the y axis. A sample data array would looke like this:
    @data = ( # First the time span [ qw(1992 1993 1994 1995 1996 1997 1998) ], # data set 1 = 1000s of people in Peoria [ 5, 10, 8, 13, 16, 24, 32], # data set 2 = 1000s of people in Santa Fe [6.5, 9.2, 14.2, 12.8, 22, 31.7, 2] );"
    GD::Graph::mixed is for graphing multiple data sets and making one points, one lines, etc. When I've used that though I've just had one x value array.
      I mentioned the O'Reilly book Programming Web Graphics with Perl & GNU Software, which is out of date, then I ran across another node while searching perlmonks that you might be interested in. A review of the newer book Graphics Programming with Perl.

      The book is by by Martien Verbruggen, author of GD::Graph.