绘制按列而不是行排列的数据

Plotting data arranged in columns rather than rows

我有一个相当简单的数据文件,它按列排列。然而, gnuplot 更喜欢行中的数据,我不知道是否有办法绘制 未经预处理的数据(我想避免)。

示例数据如下:

Time;10;20;30;40;50
Mag;100;200;400;750;950
Att;120;280;550;790;350
Sol;210;820;550;970;530

如果将数据按行排列,则绘制数据很容易:

set datafile separator ";"
set key autotitle columnheader
$DATA << EOD
Time;Mag;Att;Sol
10;100;120;210
20;200;280;820
30;400;550;550
40;750;790;970
50;950;350;530
EOD
plot $DATA using 1:2 w l, \
  '' using 1:3 w l, \
  '' using 1:4 w l

示例数据应该是这样呈现的:

有没有办法让 gnuplot 处理按列排列的数据文件?我试过 使用矩阵但无法获得预期的结果。任何有关如何解决此问题的提示。

一般来说,gnuplot 喜欢列中的数据而不是行中的数据。与您的术语相反,您的第一个示例通常称为“行中的数据”,而您的第二个示例通常称为“列中的数据”。

使用 gnuplot 绘制数据的最简单方法是转置数据。我可以理解您不想修改数据文件或使用外部工具对其进行预处理。 Linux 用户可能默认安装了 awk、sed 等工具...,Windows 用户通常没有。 不幸的是,gnuplot 没有转置函数。但是,您也可以使用 gnuplot(平台独立)来完成,可能有点麻烦,但它确实有效。

代码:

### plotting data in rows --> transpose data first
reset session

$Data <<EOD
Time;10;20;30;40;50
Mag;100;200;400;750;950
Att;120;280;550;790;350
Sol;210;820;550;970;530
EOD

set datafile separator ";"

# transpose data
stats $Data u 0 nooutput   # get the number of columns
maxCol = STATS_columns
set print $DataTransposed
    do for [i=1:maxCol] {
        Line = ''
        set table $Dummy
            plot $Data u (Line=Line.(Line ne '' ? ';' : '').strcol(i),'') w table
        unset table
        print Line
    }
set print

set key autotitle columnheader
set key top left

plot $DataTransposed using 1:2 w l, \
     '' using 1:3 w l, \
     '' using 1:4 w l
### end of code

结果: