Gnuplot - 无法正确绘制两个不同大小的矩阵

Gnuplot - Unable to properly plot two matrices with different sizes

我的数据文件如下:

#smaller matrix
5   0.00    0.25    0.50    0.75    1.00
0.00    1   2   3   4   5
0.25    5   4   3   2   1
0.50    1   2   3   4   5
0.75    5   4   3   2   1
1.00    1   2   3   4   5


#bigger matrix
8   0.00    0.25    0.50    0.75    1.00    1.25    1.50    1.75
0.00     1  2   3   4   5   6   7   8
0.25    5   4   3   2   1   0   1   2
0.50    1   2   3   4   5   6   7   8
0.75    5   4   3   2   1   0   1   2
1.00    1   2   3   4   5   6   7   8
1.25    5   4   3   2   1   0   1   2
1.50    1   2   3   4   5   6   7   8
1.75    5   4   3   2   1   0   1   2

gnuplot脚本如下:

set term png size 900, 400
set output 'matrices-test.png'
set multiplot layout 1,2

set xrange [0:1.0]
set yrange [0:1.0]
plot 'matrices-test' index 0 nonuniform matrix w image

set xrange [0:1.75]
set yrange [0:1.75]
plot 'matrices-test' index 1 nonuniform matrix w image

unset multiplot

当我 运行 这个脚本时,较小的矩阵出现扭曲。图像倾斜,数据似乎损坏:

如果我将每个矩阵放入一个单独的文件中,它们都可以毫无问题地绘制出来。

我是不是做错了什么?或者这是 gnuplot 中的错误? (我使用的是 Ubuntu Linux 中的最新版本 5.4.1。)

您遇到了程序错误。 您的脚本在开发版本 (gnuplot 5.5) 中工作正常,修复将包含在下一个 5.4 稳定版本中。

解决方法:

该错误与输入文件中存在多个数据块有关。如果矩阵位于单独的文件中,或者如果它们被加载到单独的数据块中并从那里绘制,则不会触发它。因此,一个解决方法是 select 从外部输出第一个数据块:

unset key
set auto noextend

set multiplot layout 1,2

plot '<head --lines=7 matrices.dat' index 0 nonuniform matrix w image
plot 'matrices.dat' index 1 nonuniform matrix w image

unset multiplot

由于 Ethan 的解决方案包含特定的 Linux 命令 '<head --lines=7 matrices.dat'),让我添加一个独立于平台的 gnuplot-only 解决方法。但是,它仅限于 gnuplot 版本 >=5.2.7,因为在早期版本中 strcol() 被限制为大约。仅 63 个字符。好吧,只要行少于 63 个字符,它可能适用于早期版本。

代码:

### plot two matrices from same file
# requires gnuplot >=5.2.7
reset session

$Data <<EOD
#smaller matrix
5   0.00    0.25    0.50    0.75    1.00
0.00    1   2   3   4   5
0.25    5   4   3   2   1
0.50    1   2   3   4   5
0.75    5   4   3   2   1
1.00    1   2   3   4   5


#bigger matrix
8   0.00    0.25    0.50    0.75    1.00    1.25    1.50    1.75
0.00    1   2   3   4   5   6   7   8
0.25    5   4   3   2   1   0   1   2
0.50    1   2   3   4   5   6   7   8
0.75    5   4   3   2   1   0   1   2
1.00    1   2   3   4   5   6   7   8
1.25    5   4   3   2   1   0   1   2
1.50    1   2   3   4   5   6   7   8
1.75    5   4   3   2   1   0   1   2
EOD

set datafile separator "\n"
set table $Matrix1
    plot $Data u (strcol(1)) index 0 w table
unset table
set datafile separator whitespace

set size ratio 1
unset key

set multiplot layout 1,2
    set xrange [-0.25:1.25]
    set yrange [-0.25:1.25]
    plot $Matrix1 nonuniform matrix w image
    
    set xrange [-0.25:2.0]
    set yrange [-0.25:2.0]
    plot $Data index 1 nonuniform matrix w image
unset multiplot
### end of code

结果: