如何将数据重新排列为 GD::Graph 的 (x,y) 坐标?

发布于 2024-07-16 11:41:58 字数 565 浏览 10 评论 0原文

我正在编写一个程序,它接受用户的输入文件。 该文件中有一堆数字,我将读取文件中的数字并使用 GD::图表

文件的第一行是 X 轴,文件的第二行是 X 轴对应的 Y 值以及第三、第四……等 例如:

1 2 3 4 5 
2 4 5 10 14
5 6 8 12 13

所以在上面,第一行是 x 轴,第二行是 y与 xaxis 对应的值,因此这将获取 10 个点。 (1, 2) (1, 5) (2, 4) (2, 6)....(4,10) (4,12) (5,14) (5, 13)

我计划阅读每一行数组,然后按空格或制表符分割行并将值存储在数组中。 因此,数组 1 将具有 x 轴,数组 2 将具有 y 轴,但是我应该如何在数组中存储第 3、4、5、...等行,以便它们变为 (x,y)?

此外,如何找到第一行和第二行(2 个数组)的最大值,以便为 X 轴和 Y 轴创建限制?

I am writing a program that takes in an input file from the user. The file has bunch of numbers in it and I will read the numbers in the file and create a plot based on those numbers using GD::Graph.

The first line of the file is X axis, second line of the file is Y values corresponding to X axis and third, fourth, ..., etc For example:

1 2 3 4 5 
2 4 5 10 14
5 6 8 12 13

So in the above, first line is x-axis, second is y values corresponding to xaxis so this will fetch 10 points. (1, 2) (1, 5) (2, 4) (2, 6)....(4,10) (4,12) (5,14) (5, 13)

I plan on reading each line of the array and then splitting the line on spaces or tabs and storing the values in an array. So, array 1 will have x-axis, array2 will have y-axis, but how should I store 3rd, 4th, 5th, ..., etc lines in an array so they become (x,y)?

Furthermore, how can I find the largest value for the first and second lines (2 arrays) so I can create a limit for my X and Y axes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愚人国度 2024-07-23 11:41:58

并不是您问题的真正答案,但不要错过 GD::Graph: :数据

除非您确定每行的第一行和最后一行是最小/最大的,否则您需要使用类似 List::Utilmin()max()


我不太明白你所说的“文件的第一行是X轴,文件的第二行是Y轴,第三行、第四行……等是X轴的对应点”是什么意思。

Not really an answer to your question, but don't miss GD::Graph::Data.

Unless you are sure that the first and last on each line is smallest/biggest you'll need to use something like List::Util's min() and max().


I don't really understand what you mean by "The first line of the file is X axis, second line of the file is Y axis and third, fourth, ..., etc are corresponding points to X axis."

江南烟雨〆相思醉 2024-07-23 11:41:58

您可以随时增加 x 和 y 数组。

#!/usr/bin/perl

use Data::Dumper;
use warnings;
use strict;

my @xs = ();
my @ys = ();
my $expecting_xs = 1;
my $last_xs_count;

while(<>) {
  chomp;
  my @values = split(/\s+/);
  if($expecting_xs) {
    push(@xs, @values);
    $last_xs_count = @values;
    $expecting_xs = 0;
  } else {
    if(@values != $last_xs_count) {
      die "Count mismatch";
    } 
    push(@ys, @values);
    $expecting_xs = 1;
  }
}

if(!$expecting_xs) {
  die("Odd number of lines");
}

my($xmin, $xmax) = extremes(@xs);
my($ymin, $ymax) = extremes(@ys);

print "xmin: $xmin xmax: $xmax ymin: $ymin ymax: $ymax\n";
print Dumper(\@xs), Dumper(\@ys);

sub extremes {
  my(@values) = @_;
  return undef unless @values;
  my $min = shift(@values);
  my $max = $min;
  for my $value (@values) {
    $max = $value if $value > $max;
    $min = $value if $value < $min;
  }
  return $min, $max;
}

You can grow your x and y arrays as you go.

#!/usr/bin/perl

use Data::Dumper;
use warnings;
use strict;

my @xs = ();
my @ys = ();
my $expecting_xs = 1;
my $last_xs_count;

while(<>) {
  chomp;
  my @values = split(/\s+/);
  if($expecting_xs) {
    push(@xs, @values);
    $last_xs_count = @values;
    $expecting_xs = 0;
  } else {
    if(@values != $last_xs_count) {
      die "Count mismatch";
    } 
    push(@ys, @values);
    $expecting_xs = 1;
  }
}

if(!$expecting_xs) {
  die("Odd number of lines");
}

my($xmin, $xmax) = extremes(@xs);
my($ymin, $ymax) = extremes(@ys);

print "xmin: $xmin xmax: $xmax ymin: $ymin ymax: $ymax\n";
print Dumper(\@xs), Dumper(\@ys);

sub extremes {
  my(@values) = @_;
  return undef unless @values;
  my $min = shift(@values);
  my $max = $min;
  for my $value (@values) {
    $max = $value if $value > $max;
    $min = $value if $value < $min;
  }
  return $min, $max;
}
甜嗑 2024-07-23 11:41:58

哎呀,误读了问题,您想要 AoAoH 或 AoH,具体取决于第一条线之后的每条线是否代表一条线,或者都只是分别绘制的点。 如果文件中的每一行都成为图中的一条线,我会这样写:

#!/usr/bin/perl

use strict;
use warnings;
use List::Util qw/min max/;

my @x_points         = split " ", scalar <>; #read in the x axis labels
my ($x_min, $x_max)  = (sort { $a <=> $b } @x_points)[0,-1];
my ($y_min, $y_max)  = (0, 0);

#lines is an AoAoH, first layer are the lines to be drawn
#second layer is a list of coords
#third layer are the x and y coords
my @lines;
while (<>) {
    my @y_points = split;
    #if the two arrays are not the same size, we have a problem
    die "invalid file\n" unless @y_points == @x_points;

    $y_min = max($y_min, @y_points);
    $y_max = min($y_max, @y_points);

    push @lines, [ 
        map { { x => $x_points[$_], y => $y_points[$_] } }  
    0 .. $#x_points 
    ];
}

use Data::Dumper;

print "x min and max $x_min $x_max\n",
      "y min and max $y_min $y_max\n",
      "data:\n",
      Dumper(\@lines);

my $i;
for my $line (@lines) {
    $i++;
    print "line $i is made up of points: ",
        (map { "($_->{x}, $_->{y}) " } @$line), "\n";
}

如果它们只是要绘制的点,我会这样处理它:

#!/usr/bin/perl

use strict;
use warnings;
use List::Util qw/min max/;

my @x_points         = split " ", scalar <>; #read in the x axis labels
my ($x_min, $x_max)  = (sort { $a <=> $b } @x_points)[0,-1];
my ($y_min, $y_max)  = (0, 0);

#lines is an AoAoH, first layer are the lines to be drawn
#second layer is a list of coords
#third layer are the x and y coords
my @points;
while (<>) {
    my @y_points = split;
    #if the two arrays are not the same size, we have a problem
    die "invalid file\n" unless @y_points == @x_points;

    $y_min = max($y_min, @y_points);
    $y_max = min($y_max, @y_points);

    push @points,
        map { { x => $x_points[$_], y => $y_points[$_] } }
        0 .. $#x_points;
}

use Data::Dumper;

print "x min and max $x_min $x_max\n",
      "y min and max $y_min $y_max\n",
      "data:\n",
      Dumper(\@points);

print "Here are the points: ", 
    (map { "($_->{x}, $_->{y}) " } @points), "\n";

Whoops, misread the question, you want either an AoAoH or an AoH, depending on whether each line after the first represents a line or the are all just points to be plotted respectively. Here is how I would write it if each line in the file was to become a line in the graph:

#!/usr/bin/perl

use strict;
use warnings;
use List::Util qw/min max/;

my @x_points         = split " ", scalar <>; #read in the x axis labels
my ($x_min, $x_max)  = (sort { $a <=> $b } @x_points)[0,-1];
my ($y_min, $y_max)  = (0, 0);

#lines is an AoAoH, first layer are the lines to be drawn
#second layer is a list of coords
#third layer are the x and y coords
my @lines;
while (<>) {
    my @y_points = split;
    #if the two arrays are not the same size, we have a problem
    die "invalid file\n" unless @y_points == @x_points;

    $y_min = max($y_min, @y_points);
    $y_max = min($y_max, @y_points);

    push @lines, [ 
        map { { x => $x_points[$_], y => $y_points[$_] } }  
    0 .. $#x_points 
    ];
}

use Data::Dumper;

print "x min and max $x_min $x_max\n",
      "y min and max $y_min $y_max\n",
      "data:\n",
      Dumper(\@lines);

my $i;
for my $line (@lines) {
    $i++;
    print "line $i is made up of points: ",
        (map { "($_->{x}, $_->{y}) " } @$line), "\n";
}

And here is how I would handle it if they are just points to be ploted:

#!/usr/bin/perl

use strict;
use warnings;
use List::Util qw/min max/;

my @x_points         = split " ", scalar <>; #read in the x axis labels
my ($x_min, $x_max)  = (sort { $a <=> $b } @x_points)[0,-1];
my ($y_min, $y_max)  = (0, 0);

#lines is an AoAoH, first layer are the lines to be drawn
#second layer is a list of coords
#third layer are the x and y coords
my @points;
while (<>) {
    my @y_points = split;
    #if the two arrays are not the same size, we have a problem
    die "invalid file\n" unless @y_points == @x_points;

    $y_min = max($y_min, @y_points);
    $y_max = min($y_max, @y_points);

    push @points,
        map { { x => $x_points[$_], y => $y_points[$_] } }
        0 .. $#x_points;
}

use Data::Dumper;

print "x min and max $x_min $x_max\n",
      "y min and max $y_min $y_max\n",
      "data:\n",
      Dumper(\@points);

print "Here are the points: ", 
    (map { "($_->{x}, $_->{y}) " } @points), "\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文