Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

gnuplot: label x and y-axis of matrix (heatmap) with row and column names

I'm absolutely new to gnuplot and did not find a working solution after googling.

I have a data matrix looking something like this:

  A B C D E
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 plotting a heatmap with plot 'result.csv' matrix with image, with the x-axis labeled with A-E and the y-axis labeled with A-E. This matrix does not always have the same size, but the number of row always equals the number of cols and it's always labeled.

Could someone help me out with this? I guess I have to use the using command, but up to now this appears like complete voodoo to me...

Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This one is actually a doosie and I can't make it happen without some *nix shell magic.

First, we want to get the x tic labels and y tic labels:

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

At this point, XTICS is the string "F G H I J" and YTICS is the string "A B C D E".

Now, we want to set the 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've used 2 gnuplot builtin functions (word and words). words(string) counts how many words there are in the given string (a word is a character sequence separated by spaces). word(string,n) returns the n'th word in the string.

Now, we can plot your datafile ... The only problem is that matrix wants to use all rows and columns in your datafile. You might be able to cut down the rows/columns actually read by using the every keyword, but I don't know how to do that on matrix files -- and I think it's probably easier to just keep on relying 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 to.

Also note that since we have used iteration, this script will only work in gnuplot 4.3 or higher -- Since the current stable is 4.6, hopefully that's Ok.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...