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

Hi, I have a 2D array where each element is a dot or a space. Along the margins I have two strings that I compare and print a dot if there is a match. The output file is too large to attach but right now it's printing to a text file.

I would like to know is it possible to use GD to either convert the file to a graph or just create a graph directly so that the plot could be viewed in a single window without the dots being made smaller (i.e. reduce the whitespace in the file so that the dots would be closer together in a single graph). I could probably make the x- and y- axes numerical using the length of the strings but the strings are ~1500 characters long so I would need to compress this.

I have looked at GD and tried to achieve it but couldn't get it to work. I would be very grateful if you could provide some guidance on how to achieve this if it is possible.

Replies are listed 'Best First'.
Re: GD module help please
by BrowserUk (Patriarch) on Mar 05, 2011 at 19:17 UTC

    Given the data you describe, this will plot a black pixel for the dots and a white pixel for the spaces:

    #! perl -slw use strict; use GD; my @d2D = map[ map{ rand > 0.2 ? ' ' : '.'} 1 .. 1500 ], 1 .. 1500; my $i = GD::Image->new( 1500, 1500, 0 ); my $black = $i->colorAllocate( (0)x3 ); my $white = $i->colorAllocate( (255)x3 ); for my $y ( 0 .. 1499 ) { for my $x ( 0 .. 1499 ) { $i->setPixel( $x, $y, $d2D[ $y ][ $x ] eq ' ' ? $white : $black ); } } open PNG, '>:raw', "$0.png" or die $!; print PNG $i->png; close PNG; system 1, "$0.png";

    Update: looking back at a previous post, perhaps this is what you are trying to do?

    #! perl -slw use strict; use GD; my $seq1 = join'',map{ (qw[ a c g t ] )[ rand 4 ] } 1 .. 1500; my $seq2 = join'',map{ (qw[ a c g t ] )[ rand 4 ] } 1 .. 1500; my $i = GD::Image->new( 1500, 1500, 0 ); my $black = $i->colorAllocate( (0)x3 ); my $white = $i->colorAllocate( (255)x3 ); for my $y ( 0 .. 1499 ) { for my $x ( 0 .. 1499 ) { $i->setPixel( $x, $y, substr( $seq1, $y, 1 ) eq substr( $seq2, $x, 1 ) ? $black : $white ); } } open PNG, '>:raw', "$0.png" or die $!; print PNG $i->png; close PNG; system 1, "$0.png";

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: GD module help please
by chrestomanci (Priest) on Mar 05, 2011 at 20:33 UTC

    You could try GD::Graph. It has a number of sub classes for drawing different types of graph. From you description it sounds like you want a scatter graph, so you need GD::Graph::points.

    All those graphing modules expect you to provide some arrays of data, and some other arguments representing the labels for each axis, what colours you want and the like. They return a GD bitmap object which you can save to disc or emit into a web page as part of a CGI script. You can probably also include it in a GUI, though I have not personally attempted to do that.

    About 10 years ago, I was using the GD::Graph modules in a set of reporting scripts that I ran daily for the company I was working for at the time. Also contributed some patches. I have not used those modules since then.

Re: GD module help please
by Anonymous Monk on Mar 05, 2011 at 18:39 UTC