Dear all,
I programmed a function which plots a nice correlation heatmap between
various variables. However, I would like to neglect "nan" in the matrix.
I sent gnuplot the command "printf "set datafile missing 'nan'\n"
but it
doesn't work, and I still obtain the error: "warning: matrix contains
missing or undefined values. Btw, the plot is compiled nicely but I
can't get rid of this error message.
I am using gnuplot 5.0 patchlevel 4, but I remember that this issue
already occurred in previous version. Has anyone a clue?
Artur
<hansl>
clear
set echo off
set messages off
function void corrheat (matrix M, strings name,
string fname "Path and name of the figure",
int fontsize "Set font size in pt",
int color[0:5:4] "Select color")
set warnings off
string path = "heatmap.gp"
if $windows
sprintf tmpfile "@dotdir\@path"
else
sprintf tmpfile "@dotdir/@path"
endif
outfile @tmpfile --write
printf "set encoding utf8 \n"
printf "set nokey \n"
printf "set term pdfcairo font 'Helvetica,%d'\n", fontsize
# Put the border more to the background by applying it
# only on the left and bottom part and put it and the tics in gray
printf "set style line 11 lc rgb \"#808080\" lt 1 \n"
printf "set border 3 back ls 11\n" # get rid of upper + left border
printf "set tics nomirror \n"
if color==1
printf "set palette defined ( 0 '#000fff', 1 '#90ff70', 2
'#ee0000') \n"
elif color==2
# MATLAB COLORS:
printf "set palette defined (0 0 0 0.5, 1 0 0 1, 2 0 0.5 1, 3 0
1 1, 4 0.5 1 0.5, 5 1 1 0, 6 1 0.5 0, 7 1 0 0, 8 0.5 0 0)\n"
elif color==3
printf "set palette maxcolors 10 \n"
elif color==4
printf "set palette rgbformulae 22,13,10 \n"
elif color==5
printf "set palette grey\n"
endif
#printf "set palette negative \n" # reverse colorbar
# Reverse row and columns values
M = mreverse(M) #reverse rows
M = mreverse(M')'#reverse cols
scalar count = nelem(name)-1
printf "set ytics ("
loop i=1..nelem(name) -q
if i<nelem(name)
printf "\"%s\" %d, ", name[i], count
else
printf "\"%s\" %d", name[i], count
endif
count--
endloop
printf " ) font 'Helvetica,%d' out \n", fontsize #rotate by 45
scalar count = nelem(name)-1
printf "set xtics ("
loop i=1..nelem(name) -q
if i<nelem(name)
printf "\"%s\" %d, ", name[i], count
else
printf "\"%s\" %d", name[i], count
endif
count--
endloop
printf " ) font 'Helvetica,%d' out \n", fontsize #rotate by 45
printf "set xtics rotate by 45 right\n"
printf "set datafile missing 'nan'\n"
#printf "set datafile missing '0'\n"
printf "set datafile separator ' ' \n" # Discard empty entries
#add a slight grid to make it easier to follow the exact position
of the curves
printf "set style line 12 lc rgb \"#808080\" lt 0 lw 1 \n" #
light
grey color
#printf "set grid back ls 12\n"
printf "set style fill transparent solid 0.8 noborder \n" # set
lighter shaded area
# HEATMAP
printf "plot \\\n"
printf "'-' using 1:2:3 matrix with image\n"
loop i=1..rows(M) -q
loop j=1..cols(M) -q
if M[i,j]<0 || M[i,j]>0
printf "%.6f", M[i,j]
else
printf "nan"
endif
if j<cols(M)
printf ", "
endif
endloop
printf "\n"
endloop
printf "e \n"
outfile --close
gnuplot --input="@tmpfile" --output="@fname"
end function
#---------
# Example
#---------
open denmark.gdt -q
list y = LRM LRY IBO IDE
list y = y diff(y)
corr y
M = lower( mcorr({y}) )
M = (M ? M : NA)
matrix Obs = seq(1,rows(M))
string xlab = ""
string ylab = ""
strings names = varnames(y)
string fname = "(a)workdir/corrcheck.pdf"
scalar fs = 13
scalar color = 4
corrheat (M, names, fname, fs, color)
</hansl>