Saturday, May 9, 2020

Gnuplot - Matrix Uniform Data

It is a common Matrix with either rowheader/columnheader and data. Format and example of the matrix with row and column header is 


There may also exists few matrix that doesn't have the headers.  Now, Lets plot it.  Matrix and command

num.dat:

,Apple,Bacon,Cream,Donut,Eclair
Row1, 	5, 4, 3, 1, 0
Row2, 	2, 2, 0, 0, 1
Row3, 	0, 0, 0, 1, 0
Row4, 	0, 0, 0, 2, 3
Row5, 	0, 1, 2, 4, 3

Script:

set datafile separator ","
set palette defined (0 "white", 5 "green")
plot 'num.dat' matrix rowheaders columnheaders  with image

Palette when defined with 0 as white and 5 as green.  It is the basic heatmap.  Any values that are in between will have proportionate white and green colors in them.  Resultant graph will be:


Lets draw a colorful graph.  Here is the data and the script.

num.dat:

,Apple,Bacon,Cream,Donut,Eclair
Row1, 	5, 4, 3, 7, 5
Row2, 	3, 8, 6, 8, 6
Row3, 	6, 7, 5, 7, 5
Row4, 	3, 5, 3, 8, 3
Row5, 	6, 7, 5, 4, 3

Script:

set datafile separator ","
set palette defined (3 "blue",5 "red",8 "green")
plot 'num.dat' matrix rowheaders columnheaders  with image

You see the colorful graph resulted.


NOTE: My palette is defined from 3 to 8.  If your data got any value not within 3 and 8.  The palette adjusts itself and becomes messy like the one below.  Intentionally I inserted 0 in the table and see the palette stretched itself from 0 to 8.


Using 'using'

The following is the demonstration of how we can use 'using' command.  For this purpose we use the uniform matrix without any row or column headers.
num.dat:

5, 4, 3
2, 8, 0

Script:

set datafile separator ","
plot 'num.dat' matrix using 1:2:(sprintf("%g",$3)) with labels

The resultant graph contains the x,y matrix positions as xtics and ytics.The values are printed using sprintf command.
Now, we plot 4 copies of same matrices at 4 different positions.  Labelled with green is the matrix at default position.  Red Labelled will be moved 4 position away from x-axis.  Blue Labelled will be moved 3 positions above from y-axis.  Violet labelled will be moved away both on x-axis and y-axis.  The metric is mentioned in the using command.

plot 'num.dat' matrix using 1:2:(sprintf("%g",$3)) with labels textcolor rgb "green",\
'num.dat' matrix using ($1+4):2:(sprintf("%g",$3)) w labels tc rgb "red",\
'num.dat' matrix using 1:($2+3):(sprintf("%g",$3)) w labels tc rgb "blue",\
'num.dat' matrix using ($1+4):($2+3):(sprintf("%g",$3)) w labels tc rgb "violet"

Result is:

No comments:

Post a Comment