# Using GNUplot to create a heatmap in pgfplots

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 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

and have tried

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


which resulted in

MWE:

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

\begin{tikzpicture}

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

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

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

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


• 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 May 21 '19 at 16:15

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}
\pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
\let#4\pgfplotsretval
}
\begin{document}
\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}
{
\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]