Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Plotting a Hash Key-value pair

by krish28 (Acolyte)
on Sep 27, 2010 at 22:39 UTC ( #862314=perlquestion: print w/ replies, xml ) Need Help??
krish28 has asked for the wisdom of the Perl Monks concerning the following question:

Hey everyone,

I need some help with plotting a histogram of a hash's keys vs their values. In my code, i have a hash whose Keys (>60,700 at last count) are numbers and the Values for these is the number of times the Key appears in the file that i am reading

With this data, i want to plot the Keys in the X" and their Values in the Y", preferably like a Histogram. Currently i just use the GD::Graph::Histogram module, which tries to plot JUST my hash's Keys vs their counts(hash's values not used). But its not working successfully and i keep getting the error "Horizontal size too small, vertical size too small" etc if i try changing the parameters. Here is what i have in my code

$bins = int(($keys[$#keys]-$keys[1])/$histowindowsize); ###histowindowsize is some integer (i have tried 1000 and got errors) my $graph = new GD::Graph::histogram(800,600); $graph -> set( x_label => 'Hash's Keys', y_label => 'count of keys in file', title => 'Key count histogram', histogram_bins => $bins, x_labels_vertical => 1, bar_spacing => 0, shadow_depth => 1, shadowclr => 'dred', transparent => 0, ) or warn $graph -> error; my $gd = $graph -> plot (\@keys) or die $graph->error; open (IMG, '>histo.png') or die $!; binmode IMG; print IMG $gd -> png;

Should i try a different module instead or change my methodology? I want a Histogram image file as the output, but i am at my wit's end on how to get it. I'd appreciate any help..... Thanks in advance!!!!

Comment on Plotting a Hash Key-value pair
Download Code
Re: Plotting a Hash Key-value pair
by Khen1950fx (Canon) on Sep 28, 2010 at 05:07 UTC
    Stay with GD::Graph::histogram. I made a few adjustments, so this should work for you. You'll need to rework your $bins.
    #!/usr/bin/perl use strict; use warnings; use GD::Graph::histogram; my @keys = (100, 200, 300); my $graph = new GD::Graph::histogram(800,600); $graph -> set( x_label => 'Hash Keys', y_label => 'count of keys in file', title => 'Key count histogram', x_labels_vertical => 1, bar_spacing => 0, shadow_depth => 1, shadowclr => 'dred', transparent => 0, ) or warn $graph->error; my $gd = $graph->plot(\@keys) or die $graph->error; open(IMG, '>histo.png') or die $!; binmode IMG; print IMG $gd->png;
Re: Plotting a Hash Key-value pair
by martin (Pilgrim) on Sep 28, 2010 at 05:13 UTC
    Are you sure you want to get a histogram with >60000 bars? Even with one-pixel wide bars this would fill a very big space. You might be able to do that with Gnuplot. CPAN has an interface to it: Chart::Gnuplot. See also ISBN 0380756668.

    If what you really want to get is a histogram with a much smaller number of bars, you'd have to insert some sort of aggregation. You could add the values of 1000 adjacent keys (numerically sorted) into a new bucket each, say, and plot the accumulated values.

      Are you sure you want to get a histogram with >60000 bars? Even with one-pixel wide bars this would fill a very big space.

      As far as I understand, the number of actually drawn histogram bars is determined by the histogram_bins option, not by the number of input values.

      That said, I agree that GD::Graph has some severe limitations (like the inability to do subpixel rendering), and should usually only be used for quick-and-dirty solutions, or when no high quality solution is available.

      Apart from gnuplot there's also Chart::Clicker, which I haven't used yet, but I've heard many people speak well of it.

      Perl 6 - links to (nearly) everything that is Perl 6.
      Thanks for replying... I actually binned my data into buckets and plotted it, but i end up having to use binning sizes that are much bigger than i'd prefer.. I will check out chart::gnuplot, thanks...
        hey,

        Thanks for replying guys. I tried using Chart::Gnuplot and put it all in as it was supposed to be put, but i got an esoteric error message "/tmp/Mcr2eoH11Z,plot", line 1: undefined variable: noenhanced". Any help for this??

        I also tried using Chart::Clicker, but i lost heart before i could even start, after seeing the huge list of pre-requisites and pre-requisites for those pre-requisites, that i had to install before i used the module.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-10-01 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (391 votes), past polls