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!
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.
ok, this question is old, but there is a much easier way implemented in gnuplot already:
plot 'result.csv' matrix rowheaders columnheaders with image
got it from http://gnuplot.sourceforge.net/demo/heatmaps.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With