TikZ-PGF – How to Create a Color Map Contour Plot from Matrix Data

pgfplotstabletikz-pgf

I have

succeeded plotting list data, generated from Matlab, as a contour in tikz-pgf using gnuplot. The data is arranged as

1,-6.2832,-6.2832,0.74038
2,-6.2832,-6.1563,0.74997
3,-6.2832,-6.0293,0.75982
4,-6.2832,-5.9024,0.76986
.......

The addplot works with

\addplot3[mesh/rows=100,mesh/num points=10000,contour gnuplot={
                    number=24,
                    % cdata should not be affected by z filter:
                    output point meta=rawz,
                    labels=false,
                }] table [x index={1}, y index={2}, z index={3}] {\datatable};

resulting contour[1]]

The question:

I want to do the same using matrix data, as that seems to run much faster. How do I do that, how do I have to format the data? Here the not quite minimal MWE:

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage[dvipsnames]{xcolor}

\usepackage{tikz,pgfplots,pgfplotstable,pgffor,pgfmath,tikz-3dplot}

%===================================================
\usetikzlibrary{positioning,plotmarks,pgfplots.colormaps,external,3d,calc,arrows}
\usetikzlibrary{decorations,decorations.pathmorphing,decorations.pathreplacing}
\usetikzlibrary{shapes,arrows}
\tikzexternalize[mode=convert with system call,shell escape=-enable-write18]
%===================================================

%==== Tikz & PGF Camera alignment macro ============
% Style to set TikZ camera angle, like PGFPlots `view`
\tikzset{viewport/.style 2 args={
    x={({cos(-#1)*1cm},{sin(-#1)*sin(#2)*1cm})},
    y={({-sin(-#1)*1cm},{cos(-#1)*sin(#2)*1cm})},
    z={(0,{cos(#2)*1cm})}
}}
%===================================================

%Define standard arrow tip
\tikzset{>=stealth',}

% Color Map
\pgfplotsset{
    colormap={colormap2}{
        color(0cm)=(blue!70!black);
        color(1cm)=(cyan!60!black)
    }
}

%==== Externalisation ==============================
\makeatletter
\newcommand{\useexternalfile}[2][Tikz] % \useexternalfile[Folder]{Filename}
{
    \tikzsetnextfilename{#2-output}
    \input{#1/#2.tikz}
}
\makeatother
%===================================================

\begin{document}

\begin{tikzpicture}

    \pgfplotstableread[col sep = comma]{Matrix.csv}\datatable

    \begin{axis}
    [
        view={0}{90},
        scale only axis,
        colormap name={colormap2},
        ticks=none
    ]
        \addplot3[surf, mesh/rows=100, mesh/cols=100] table {\datatable};

    \end{axis}

\end{tikzpicture}

\end{document}

Using this, I get the error

Package pgfplots Error: the arguments of [mesh/rows=100,mesh/cols=100] assume 10000 points, but I got actually N = 100 points!
The data matrix appears to be incomplete or overcomplete!?

where my Matrix.csv is of the form

0.74038,0.74228,0.744,0.74549,0.74672,......,0.27529
0.74997,0.75246,0.75477,0.75685,0.75867,.....,0.26786
.....

i.e. every line has 100 points of data and there are 100 lines. Data in one line is separated by comma and lines are separated by newline.

Best Answer

Your original input is the only and correct supported way how to communicate matrix data to pgfplots, i.e. by providing one matrix cell per row.

A format which provides the Z data as matrix in which each line in the input file is one row of the entire matrix is currently unsupported.