Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^5: Is this the correct structure?

by poj (Priest)
on Jul 05, 2013 at 13:04 UTC ( #1042703=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Is this the correct structure?
in thread Is this the correct structure?

I couldn't see how to do stacked bars with Chart::Gnuplot. This example uses GD::Graph.

#!perl use strict; use GD; use GD::Graph::bars; use GD::Graph::hbars; # read header for y legend chomp( my $header = <DATA> ); my (undef,@y_legend) = split /\s+/,$header; # read rows of data my @data=(); while (<DATA>){ chomp; my @col = split /\s+/,$_; for my $i (0..$#col){ push @{$data[$i]},$col[$i]; } } # create vertical stacked bars graph # use hbars->new for horiz stacked bars my $my_graph = GD::Graph::bars->new(1000,500); $my_graph->set( x_label => 'Method', y_label => 'Y label', title => 'Stacked Bar', cumulate => 2, bar_spacing => 5, transparent => 0, ); # Allowed # white, lgray, gray, dgray, black, lblue, blue, dblue, gold, lyellow, + yellow, # dyellow, lgreen, green, dgreen, lred, red, dred, lpurple, purple, dp +urple, lorange, # orange, pink, dpink, marine, cyan, lbrown, dbrown. my @colour_array = qw(lblue dbrown blue lred lgreen yellow green red purple orange pink dyellow); $my_graph->set( dclrs => \@colour_array ); # text sizes # gdGiantFont, gdLargeFont, gdMediumBoldFont, gdSmallFont , gdTinyFont $my_graph->set_x_label_font(gdGiantFont); $my_graph->set_x_axis_font(gdSmallFont); $my_graph->set_y_label_font(gdGiantFont); $my_graph->set_y_axis_font(gdMediumBoldFont); $my_graph->set_title_font(gdGiantFont); $my_graph->set_legend_font(gdGiantFont); $my_graph->set_legend( @y_legend ); # plot my $img = $my_graph->plot(\@data); # save open(IMG, '>file.png') or die $!; binmode IMG; print IMG $img->png; __DATA__ METHOD 0TM 1TM 2TM >2TM method1 3 68 18 10 method2 3 80 10 6 method3 3 87 9 1 method4 5 83 9 3 method5 6 75 15 4 method6 14 77 6 3 method7 12 82 2 3 method8 9 84 3 3 method9 13 68 16 3 method10 5 65 20 9 method11 9 64 20 7 method12 5 68 18 9 method13 6 82 9 2 method14 11 79 8 2 method15 6 69 20 5 method16 5 80 11 3 method17 1 68 17 14
poj


Comment on Re^5: Is this the correct structure?
Select or Download Code
Re^6: Is this the correct structure?
by Anonymous Monk on Jul 05, 2013 at 22:39 UTC
    Thank you a lot for all your trouble!
Re^6: Is this the correct structure?
by Anonymous Monk on Jul 06, 2013 at 12:01 UTC
    I am almost there with Chart::Gnuplot.
    One question still remains to be answered...
    In the final step, the:
    $chart->plot2d($h1, $h2, $h3...)

    where $h1, $h2, $h3 etc are the various datasets created with Chart::Gnuplot::DataSet->new, how can I pass the names ($h1, $h2 etc) if I have them in an array?
    My final version of the code so far is:
    #!/usr/bin/perl use Chart::Gnuplot; #one must specify FILENAME to get data from (tab-delimited), a name fo +r the output file, a type for output file (e.g. pdf, png, eps) and a +plot title ($infile, $title, $type, $plot_title) = @ARGV; $outfile = $title.'.'.$type; #add or remove colours accordingly @colour_array = ("lblue", "brown", "blue", "lred", "lgreen", "yellow", + "green", "red", "purple", "orange", "pink", "dyellow"); @data; $counter; open IN, "$infile" or die $!; $label_line = <IN>; chomp $label_line; #find the labels data (for n +aming the colours in the stacked bars) @split_labels = split(/\t/, $label_line); while(<IN>) { chomp; $counter = 0; for (split/\t/,$_) { push @{$data[$counter++]}, $_; } } close IN; #x-axis labels is the row #0 in the AoA @data @x = @{$data[0]}; $total_y = @data-1; #find how many y rows we have => how many colou +rs we need (-1 because #0 is x-axis) @pick_colours = @colour_array[ 0 .. $total_y - 1 ]; # Initiate the chart object $chart = Chart::Gnuplot->new( output => $outfile, title => $title, xlabel => { text => 'Method', color => 'black', offset => "-1", font => 'Times-Roman, 20' }, ylabel => { text => $plot_title, color => 'black', offset => -1, font => 'Times-Roman, 20' }, yrange => [0, '*'], xtics => { rotate => '-270', font => 'Times-Roman, 15' }, ytics => { font => 'Times-Roman, 15' }, bg => { color => 'white', density => 0.2, }, plotbg => 'white', legend => {position => 'outside bottom'}, border => { sides => 'bottom, left', linetype => 3, width => 2, color => 'black', }, "style histogram" => 'rowstacked' ); #all the rest rows in @data are used for creating the histogram bars #create object foreach series of data (column in excel) $counter_objects=0; @all_vars=(); for $y ( 1 .. $#data ) { $legend_name = $split_labels[$y]; #get the name for the legend, + e.g 1TM, 2TM, >2TM @tmp_y = @{$data[$y]}; #create temp array $counter_objects++; $myvar.= '$h'."$counter_objects".", "; push @all_vars, $myvar; ${h."$counter_objects"} = Chart::Gnuplot::DataSet->new ( xdata => \@x, ydata => \@tmp_y, title => "$legend_name", fill => {density => 0.2}, style => "histograms", ); } $wanted = substr ($string_of_vars,0, -2); $chart->plot2d(\@all_vars);

    but in the last step, it complaints, because:
    Not a HASH reference at /usr/local/share/perl/5.14.2/Chart/Gnuplot.pm
      You just need to create an array of datasets. Push comes after the ->new.
      #$myvar.= '$h'."$counter_objects".", "; #push @all_vars, $myvar; #${h."$counter_objects"} = Chart::Gnuplot::DataSet->new my $ds = Chart::Gnuplot::DataSet->new( xdata => \@x, ydata => \@tmp_y, title => "$legend_name", fill => {density => 0.2}, style => "histograms", ); push @all_vars, $ds; } $chart->plot2d(@all_vars); # no \
      poj
        Again dissapointment...
        I don't really get it though!! The code executes perfectly, yet I do not get a stacked bar which adds up to 100, as it should based on the input file... In some methods it even stays as low as maybe 20, for some strange reason!
        #!/usr/bin/perl use Chart::Gnuplot; #one must specify FILENAME to get data from (tab-delimited), a name fo +r the output file, a type for output file (e.g. pdf, png, eps) and a +plot title ($infile, $title, $type, $plot_title) = @ARGV; $outfile = $plot_title.'.'.$type; #add or remove colours accordingly @colour_array = ("#00308F", "#004225", "#480607", "#AF002A", "#008000" +, "#E25822", "#3B444B", "#7C0A02", "#848482", "#563C5C"); @data; $counter; open IN, "$infile" or die $!; $label_line = <IN>; chomp $label_line; #find the labels data (for n +aming the colours in the stacked bars) @split_labels = split(/\t/, $label_line); while(<IN>) { chomp; $counter = 0; for (split/\t/,$_) { push @{$data[$counter++]}, $_; } } close IN; #x-axis labels is the row #0 in the AoA @data @x = @{$data[0]}; $total_y = @data-1; #find how many y rows we have => how many colou +rs we need (-1 because #0 is x-axis) @pick_colours = @colour_array[ 0 .. $total_y - 1 ]; # Initiate the chart object $chart = Chart::Gnuplot->new( output => $outfile, title => $title, xlabel => { text => 'Method', color => 'black', offset => "-1", font => 'Times-Roman, 20' }, ylabel => { text => 'Performance', color => 'black', offset => -1, font => 'Times-Roman, 20' }, yrange => [0, '*'], xtics => { rotate => '-270', font => 'Times-Roman, 15' }, ytics => { font => 'Times-Roman, 15' }, bg => { color => 'white', density => 0.2, }, plotbg => 'white', legend => {position => 'outside bottom'}, border => { sides => 'bottom, left', linetype => 3, width => 2, color => 'black', }, "style histogram" => 'rowstacked' ); #all the rest rows in @data are used for creating the histogram bars #create object foreach series of data (column in excel) @all_vars=(); for $y ( 1 .. $#data ) { $legend_name = $split_labels[$y]; #get the name for the legend, + e.g 1TM, 2TM, >2TM @tmp_y = @{$data[$y]}; #create temp array $ds = Chart::Gnuplot::DataSet->new( xdata => \@x, ydata => \@tmp_y, title => "$legend_name", fill => {density => 0.2}, style => "histograms", color => "$pick_colours[$y-1]" ); push @all_vars, $ds; } $chart->plot2d(@all_vars);

        The code with the suggestions/corrections of the kind monks produces no errors, BUT, if you try it on the real example, which is the following dataset:
        METHOD 0TM 1TM 2TM >2TM method1 3 68 18 10 method2 3 80 10 6 method3 3 87 9 1 method4 5 83 9 3 method5 6 75 15 4 method6 14 77 6 3 method7 12 82 2 3 method8 9 84 3 3 method9 13 68 16 3 method10 5 65 20 9 method11 9 64 20 7 method12 5 68 18 9 method13 6 82 9 2 method14 11 79 8 2 method15 6 69 20 5 method16 5 80 11 3 method17 1 68 17 14

        using the command:
        perl script.pl datasets EVALUATION png MYIMAGE

        it creates a really awful graph!!!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1042703]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2015-07-05 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls