in reply to matrix 'graphic' problem

Angharad 
If one imagines the first two columns as 'objects' the third column is a measure of similarity between those two objects. I want to create a 2D grid as an image (.png/.ps file or similar)

As has been said already, map the values on an array and map this array to an arbitary graphic output, like:

... use Imager; # OPEN FILE my $fn = 'datafile.dat'; open my $fh, '<', $fn or die "$fn $!"; # READ DATA INTO 2D MATRIX my @matrix = map [ qw(-1) x 40 ], 1..40; $matrix[$_->[1]][$_->[0]] = $_->[2] for map [split /\s+/ ], <$fh>; # PREPARE IMAGE my $scale=10; my $img = Imager->new(xsize=>40*$scale, ysize=>40*$scale, channels=>3 +); $img->box(filled=>1, color=>'#FFFFFF'); # CREATE IMAGE for my $row (0 .. @matrix-1) { for my $col (0 .. @{$matrix[$row]}-1) { my $val = $matrix[$row][$col]; next if $val == -1; # SELECT APPROPRIATE COLOR (BE CREATIVE HERE ;-) $img->box( color=>[ ($val/10)*25, $val*2.5, $val ], filled=>1, xmin=>$col*$scale, ymin=>$row*$scale, xmax=>($col+1)*$scale, ymax=>($row+1)*$scale ) } } # DUMP IMAGE TO FILE $img->write(file=>'my.jpg', jpegquality=>90) or die $img->errstr; ...

(This is my first shot at Imager - after sams remark - and therefore the reason that I posted this, so feel free to give hints ...)

Regards

mwa

Modification #1:

Replies are listed 'Best First'.
Re^2: matrix 'graphic' problem
by mwah (Hermit) on Oct 15, 2007 at 20:34 UTC

    Because it's Perl, I think I may try another shot (semi-golf version):

    use Imager; my ($n, $fac) = (40, 10); # 2D-SIZE, FACTOR TO IMAGE my $fn = shift || 'datafile.dat'; open my $fh, '<', $fn or die "$fn $!"; # OPEN FILE my $img = Imager->new(xsize=>$n*$fac, ysize=>$n*$fac, channels=>3); $img->box(filled=>1, color=>'#FFFFFF'); # BACKGROUND $img->box(color=>[($_->[2]/10)*25, $_->[2]*2.5, $_->[2] ], filled=>1, xmax=>($_->[0]+1)*$fac, ymax=>($_->[1]+1)*$fac, xmin=>$_->[0]*$fac, ymin=>$_->[1]*$fac) for map [split /\s+ +/], <$fh>; $img->write(file=>'my.jpg', jpegquality=>90) or die $img->errstr;

    the 2D matrix storage is't really necessary, this can be handled in "mid-air" ...

    Regards

    mwa