5

I am trying to create a heatmap using tikz/pgfplots in latex, I however get an empty plot. I use the same command as I used in GNUplot, which was

plot 'heat-data.txt' matrix with image;

In GNUplot this results in the desired image Image created with GNUplot where 'heat-data.txt' a file is containing the z coordinates.

6   5   4   3   1   0
3   2   2   0   0   1
0   0   0   0   1   0
0   0   0   0   2   3
0   0   1   1   1   0
0   1   0   1   1   0

Since I would like to generate heat maps of data sets in a report, I'd like to do this in the same style as the rest of the report and therefor use Tikz/Pgfplots. I have tried

\addplot3[raw gnuplot]
            gnuplot{
set view map;
plot 'heat-data.txt' matrix with image};

which resulted in Generated image

and have tried

\addplot3[raw gnuplot,surf,shader=flat]gnuplot{
plot 'heat-data.txt' matrix with image;
}; 

which resulted in enter image description here

MWE:

\documentclass[tikz, crop]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false} 
\begin{document}

\begin{tikzpicture}

\begin{axis}[
colorbar right, 
colormap/viridis,
view={0}{90}
] 

%\addplot3[raw gnuplot]gnuplot{
%plot 'heat-data.txt' matrix with image;
%};

\addplot3[raw gnuplot,surf,shader=flat]gnuplot{
plot 'heat-data.txt' matrix with image;
};

\end{axis}
\end{tikzpicture}
\end{document}

1
  • Following tex.stackexchange.com/questions/212001/… you can do \begin{axis}[view={0}{90}, colorbar, colormap name=viridis] \addplot3 [raw gnuplot,surf,mesh/rows=6,mesh/cols=6] gnuplot { plot 'heat-data.txt' matrix with image; }; which results in a heat map plot.
    – Marijn
    Commented May 21, 2019 at 16:15

1 Answer 1

1

I have not much experience with gnuplot. What I can offer is something that converts your data into something that can be plotted with an ordinary matrix plot.

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{heat-data.txt}
6   5   4   3   1   0
3   2   2   0   0   1
0   0   0   0   1   0
0   0   0   0   2   3
0   0   1   1   1   0
0   1   0   1   1   0
\end{filecontents*}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.colormaps}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval
}
\begin{document}
\pgfplotstableread[header=false]{heat-data.txt}\datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
\xdef\LstX{}
\xdef\LstY{}
\xdef\LstC{}
\foreach \Y [evaluate=\Y as \PrevY using {int(\Y-1)},count=\nY] in {1,...,\numrows}
{\pgfmathtruncatemacro{\newY}{\numrows-\Y}
\foreach \X  [evaluate=\X as \PrevX using {int(\X-1)},count=\nX] in {1,...,\numcols}
{
\ReadOutElement{\datatable}{\PrevY}{\PrevX}{\Current}
\pgfmathtruncatemacro{\nZ}{\nX+\nY}
\ifnum\nZ=2
\xdef\LstX{\PrevX}
\xdef\LstY{\PrevY}
\xdef\LstC{\Current}
\else
\xdef\LstX{\LstX,\PrevX}
\xdef\LstY{\LstY,\PrevY}
\xdef\LstC{\LstC,\Current}
\fi
}
}
\edef\temp{\noexpand\pgfplotstableset{
 create on use/x/.style={create col/set list={\LstX}},
 create on use/y/.style={create col/set list={\LstY}},
 create on use/color/.style={create col/set list={\LstC}},}}
\temp
\pgfmathtruncatemacro{\strangenum}{\numrows*\numcols}
\pgfplotstablenew[columns={x,y,color}]{\strangenum}\strangetable

%\pgfplotstabletypeset[empty cells with={---}]\strangetable
\begin{tikzpicture}
\begin{axis}[colorbar]
 \addplot [
        matrix plot,
        point meta=explicit,
] table [meta=color,col sep=comma] \strangetable;
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .