I can't reproduce your output, but here is a simplified version of your code that creates an output PNG file for me (if I input some simple numbers in a file):
use strict;
use warnings;
use GD::Graph::histogram;
my $input = 'distance.txt';
unless(open(INPUT, $input)) {
die "\nCannot open input\n";
}
my @data;
while(my $line = <INPUT>) {
chomp $line;
push @data, sprintf ("%.1f\n", $line);
}
my $graph = new GD::Graph::histogram(400,600);
$graph->set(
y_label => 'Count',
x_label => 'Distances',
title => 'Histogram',
x_labels_vertical => 1,
bar_spacing => 0,
shadow_depth => 1,
shadowclr => 'dred',
transparent => 0,
)
or warn $graph->error;
my $image = $graph->plot(\@data) or die $graph->error;
open( IMG, '>' . 'out.png' ) or die $!;
binmode IMG;
print IMG $image->png;
Post a few lines of your input file (inside "code" tags).
Your $filename variable is undefined because you didn't pass any arguments to your "main" sub. @_ holds args passed to a sub. Also, you keep clobbering your @data when you assign it to @_ (which is empty).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.