Gnuplot: label x and y axis of the matrix (Heatmap) with row and column names

I am completely new to gnuplot and have not found a working solution after googling.

I have a data matrix that looks something like this:

ABCDE A 0 2 3 4 5 B 6 0 8 9 0 C 1 2 0 4 5 D 6 7 8 0 0 E 1 2 3 4 0 

What I would like to do is build a heat map with plot 'result.csv' matrix with image , with the x axis labeled AE and the y axis labeled AE. This matrix is ​​not always the same size, but the number of rows is always equal to the number of columns and is always labeled.

Can someone help me with this? I guess I should use the using command, but so far it looks like full voodoo to me ...

Thanks a lot!

+6
source share
2 answers

This is actually a doosie, and I cannot do it without any * nix shell magic.

First we want to get the x tic and ytic tags:

 XTICS="`awk 'BEGIN{getline}{printf "%s ",$1}' test.dat`" YTICS="`head -1 test.dat`" 

At this point, XTICS is the string "FGH I J", and YTICS is the string "ABCDE".

Now we want to install xtics by iteration:

 set for [i=1:words(XTICS)] xtics ( word(XTICS,i) i-1 ) set for [i=1:words(YTICS)] ytics ( word(YTICS,i) i-1 ) 

We used 2 built-in gnuplot functions ( word and words ). words(string) counts how many words are in a given string (a word is a sequence of characters separated by spaces). word(string,n) returns the nth word in a string.

Now we can build your data file ... The only problem is that matrix wants to use all the rows and columns in your data file. You may be able to shorten the rows / columns that are really read with the every keyword, but I don’t know how to do this in matrix files, and I think it might be easier to just rely on shell utilities ( awk and sed )

 plot "<awk '{$1=\"\"}1' test.dat | sed '1 d'" matrix w image #######^ replace the first field with nothing ################################## ^ delete first line 

And now your plot (hopefully) looks the way you want it.

Also note that since we used iteration, this script will only work in gnuplot 4.3 or higher. Since the current stable version is 4.6, I hope that Ok.

+11
source

ok, this question is old, but gnuplot has already implemented a much simpler way:

 plot 'result.csv' matrix rowheaders columnheaders with image 

got it from http://gnuplot.sourceforge.net/demo/heatmaps.html

+3
source

Source: https://habr.com/ru/post/916891/


All Articles