user16372530
user16372530

Reputation: 754

With gnuplot, how to plot a vector (i.e. 1xN or Nx1 matrix) as image?

How to plot a vector (i.e. 1xN or Nx1 matrix) as image? I have no problem to plot 2x2 matrices, as follows:

plot '-' matrix with image
1 2 
3 4 
e

However, the following two examples with 2x1 and 1x2 matrices fail:

plot '-' matrix with image
1 
3 
e

plot '-' matrix with image
1 2
e

For both, I get the warning:

Image grid must be at least 4 points (2 x 2).

and nothing is rendered.

Is there another way to plot the Nx1 or 1xN matrices, so that they are rendered similar to the 2x2 matrix?

"gnuplot matrix or palette using one line" might be in parts related. However, the problem there is much more complex.

Upvotes: 1

Views: 60

Answers (2)

theozh
theozh

Reputation: 26158

Here is a simple way which works also with 1xN and Nx1 matrices. It is using plotting style with boxxyerror. It works for gnuplot 5.x and with files instead of datablocks probably also for gnuplot 4.x.

Script: (works for gnuplot>=5.0.0)

### plot small matrix
reset session

$Data1 <<EOD
 1  2
 3  4
EOD

$Data2 <<EOD
 1
 3
EOD

$Data3 <<EOD
 1 2
EOD

set size ratio -1
set style fill solid 1.0
set key noautotitle
set cbrange[1:4]
set xtics  1
set ytics  1
set cbtics 1

set multiplot layout 1,3
    plot $Data1 u 1:2:(0.5):(0.5):3 matrix w boxxy lc palette z
    plot $Data2 u 1:2:(0.5):(0.5):3 matrix w boxxy lc palette z
    plot $Data3 u 1:2:(0.5):(0.5):3 matrix w boxxy lc palette z
unset multiplot
### end of script

Result:

enter image description here

Upvotes: 1

Ethan
Ethan

Reputation: 15118

This answer assumes gnuplot version 6

Sort of. A plot with image will not accept a 1-dimensional array, but you can create a 1-dimensional pixmap and plot that instead. The example below is probably more complicated than needed, but it shows the relationships between the palette, a colormap, a pixmap, and a data array. All of these are basically 1-dimensional arrays; however their syntax and use in the program varies.

# Create a 1x4 colormap named M1x4

set palette maxcolors 4
set colormap new M1x4

# Restore palette mapping that will be used for a plot
# Note: maxcolors 0 resets to the default for the current terminal
#       cbrange sets the range of values the palette will map to
set palette maxcolors 0
set cbrange [0:512]

# Unfortunately the palette values are not recalculated until something
# is plotted to use them, so make a dummy plot to force the recalcuation.
set table $junk
plot x lc palette
unset table

# Reload the entries in our named colormap with the corresponding
# mappings of data values into the current palette.
array DATA = [ 15,  127, 415, 333 ]
M1x4[1] = palette( DATA[1] )
M1x4[2] = palette( DATA[2] )
M1x4[3] = palette( DATA[3] )
M1x4[4] = palette( DATA[4] )
print M1x4

# Now define a pixmap object to be plotted
set pixmap 1 colormap M1x4 size 4, 1
set pixmap 1 at graph 0.5, graph 0.5

# Plot anything you like; the pixmap object will be part of the plot
# For the purpose of illustration, we choose something that will show
# the palette mapping in the color box.
plot x lc palette

enter image description here

Work-around for version 5 that does not use pixmaps

To work around the need for height and width both greater than 1, you could add a dummy row of values that contain NaN. These will be transparent when plotted with image, although they do affect the scaling:

plot '-' matrix with image
15 127 415 333
NaN NaN NaN NaN
e

enter image description here

Upvotes: 1

Related Questions