Havoc Crow
Havoc Crow

Reputation: 113

Gnuplot - Unable to properly plot two matrices with different sizes

My data file is as follows:

#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

The gnuplot script is as follows:

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

When I run this script, the smaller matrix comes out distorted. The image is tilted and the data seems mangled:

enter image description here

If I put each matrix into a separate file, they are both plotted without problems.

Am I doing something wrong? Or is this a bug in gnuplot? (I'm using 5.4.1, the newest version, in Ubuntu Linux.)

Upvotes: 1

Views: 70

Answers (2)

theozh
theozh

Reputation: 25734

Since Ethan's solution contains the specific Linux command '<head --lines=7 matrices.dat'), let me add a platform independent gnuplot-only workaround. However, it is limited to gnuplot version >=5.2.7 because in earlier versions strcol() was limited to approx. 63 characters only. Well, it might work with earlier versions as long as the lines have less than 63 characters.

Code:

### 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

Result:

enter image description here

Upvotes: 0

Ethan
Ethan

Reputation: 15093

You have hit a program bug. Your script works correctly in the development version (gnuplot 5.5) and the fix will be included in the next 5.4 stable release.

Work-around:

The bug has something to do with there being multiple data blocks in the input file. It is not triggered if the matrices are in separate files or if they are loaded into separate data blocks and plotted from there. So one work-around would be to select out the first data block externally:

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

enter image description here

Upvotes: 2

Related Questions