# pgfplots contour lua with data in external file

i am working on a lot of measured data stored in a file, which i want to visualize with a contour plot. There I have coordinates x and y and a value z as function of x and y, which should be used to calculate the contour lines. Since i am already working with lualatex in Texstudio and pgfplots supports calculating contour lines since version 1.18 I would like to use this feature and not have to install gnuplots.

In this example there are just 9 lines, but in reality there are about a few thousand lines, so i really would like to keep the data in an external file and not have to copy into my texfile.

% data.dat
%
% xColumn,yColumn,zColumn <--- first line
% 0,0,3
% -2,2,1
% -1,-1,2
% 2,2,1
% 1,-1,2
% -1,1,2
% 2,-2,1
% 1,1,2
% -2,-2,1
% 2,-2,1
%             <- last line


I tried to create an MWE, but even fail at this step. What I have right now ist the following:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[view={0}{90}]

\addplot3[contour lua={levels={1,2,3}}] table[x=xColumn,y=yColumn,z=zColumn, col sep=comma] {data.dat};

\end{axis}
\end{tikzpicture}

\end{document}


Every time I try to compile the document I get the error even though I added a blank line at the end of my data.dat file.

! Package pgfplots Error: Sorry, processing the input stream did not lead to en d-of-scanline markers; the generated temporary file for 'contour external' does not contain any of them (indicating that matrix structure is lost). To fix thi s, you have the following options:

• Insert end-of-scanline markers into your input data (i.e. empty lines),
• provide two of the three options 'mesh/rows=, mesh/cols=, mesh/num points='.

See the pgfplots package documentation for explanation. Type H for immediate help. ...

                                               l.11 ...Column,z=zColumn, col sep=comma] {data.dat};


I also tried usising the keys mesh/row and mesh/col or als an alternative mesh/num points, but even with that I still get errors.

Could please someone be so kind and help me out? Thank you.

Either your contour plot is a prepared contour where the data has a \point meta value - unless it is a matlab format where equal z is used like this:

\documentclass[border=1 cm]{standalone}
\usepackage{pgfplots}
\begin{filecontents}{data.dat}
xColumn,yColumn,zColumn
0,0,3
-2,2,1
-1,-1,2
2,2,1
1,-1,2
-1,1,2
2,-2,1
1,1,2
-2,-2,1
2,-2,1
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[view={0}{90}]
\addplot3[contour prepared format=matlab] table[x=xColumn,y=yColumn,z=zColumn, col sep=comma] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}


or you will be able to make a surface plot first(You do not have to, but you must be able to). To make a surface plot, you data needs to be formated like this:

<x1> <y1> <z>
<x1> <y2> <z>
<x1> <y3> <z>

<x2> <y1> <z>
<x2> <y2> <z>
<x2> <y3> <z>


With e.g.

\addplot3[surf] table {data.dat};


Edit:

Random example (I have removed commas and header just for ease)

\documentclass[border=1 cm]{standalone}
\usepackage{pgfplots}
\begin{filecontents}{data_test.dat}
0 0 3
0 2 1
0 3 2

1 0 1
1 2 2
1 3 2

2 0 1
2 2 2
2 3 1
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}%[view={0}{90}]