# Mapping with Tikz and Gnuplot in defined area

i´m trying to plot some x,y and z values from a .txt-file with gnuplot in Latex. The plotting itself works fine. The Problem is, that there should be a defined Outline surrounding my plot. Everything which is outside the Outline shouldnt be visible. So i tried to fill the area between the Outline and a Square but this doesnt work, i think that there are too much data in my Outline. Another method could be the one described here:Gnuplot 3D Heat mapping But i dont know how to put it in Latex.

So this is my code:

\RequirePackage{tikz}
\RequirePackage{pgfplots}

\documentclass{scrbook}
\usepackage{xcolor}
\usepackage{filecontents}
\RequirePackage{tikz}
\RequirePackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usepackage{tikzscale}
\begin{document}
\begin{tikzpicture}
\begin{filecontents*}{Points.dat}
#1 2 3
-0.346 0.28 207
-0.306 0.28 203
-0.266 0.28 210
-0.226 0.28 214
-0.226 0.20 244
-0.186 0.20 255
-0.266 0.12 248
-0.226 0.08 261
-0.186 0.08 246
0 0 320
0 0.3 260
-0.4 0.3 220
-0.4 0 240
\end{filecontents*}

\begin{filecontents*}{Outline.dat}
#1 2 3
-0.38 0.01 500
-0.38 0.296 500
-0.32 0.298 500
-0.217 0.299 500
-0.017 0.149 500
-0.009 0.157 500
-0.006 0.127 500
-0.002 0.093 500
-0.001 0.069 500
-0.002 0.056 500
-0.38 0.01 500
\end{filecontents*}
\begin{filecontents*}{Rand.dat}
#1 2 3
0.025 -0.025 500
0.025 0.325 500
-0.425 0.325 500
-0.425 -0.025 500
0.025 -0.025 500
\end{filecontents*}

\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}
\begin{axis}[colorbar,
xmin=-0.45,
xmax=0.05,
ymin=-0.05,
ymax=0.35,
view={0}{90},
]
\begin{pgfonlayer}{pre main}
\addplot3 [surf] gnuplot [raw gnuplot] {
set dgrid3d 40,40 spline;
splot 'Points.dat';
};
\end{pgfonlayer}

\begin{pgfonlayer}{main}
\addplot3 [no markers, very thick, name path=A] table []{Outline.dat};
\addplot3 [black,no markers, very thick, name path=B] table []{Rand.dat};
%\addplot3 [white] fill between[of=A and B];
\end{pgfonlayer}

\end{axis}

\end{tikzpicture}
\end{document}


Thanks a lot!

• I guess it is mainly a conceptual thing: you need to draw these contours with \addplot instead of \addplot3 for the contours.
– user121799
Nov 4, 2018 at 18:45

I'd rather clip the unwanted regions away than overpainting them white. And, as I said in my comment, you need to use \addplot instead of \addplot3 since you only want a 2D path for the boundary. And you do not need fillbetween if you clip, the IMHO extremly undervoted use path trick is enough for that.

\RequirePackage{tikz}
\RequirePackage{pgfplots}

\documentclass{scrbook}
\usepackage{xcolor}
\usepackage{filecontents}
\RequirePackage{tikz}
\RequirePackage{pgfplots}
%\usepgfplotslibrary{fillbetween}
\usepackage{tikzscale}
\makeatletter % https://tex.stackexchange.com/a/38995/121799
\tikzset{
use path/.code={\pgfsyssoftpath@setcurrentpath{#1}}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{filecontents*}{Points.dat}
#1 2 3
-0.346 0.28 207
-0.306 0.28 203
-0.266 0.28 210
-0.226 0.28 214
-0.226 0.20 244
-0.186 0.20 255
-0.266 0.12 248
-0.226 0.08 261
-0.186 0.08 246
0 0 320
0 0.3 260
-0.4 0.3 220
-0.4 0 240
\end{filecontents*}
\begin{filecontents*}{Outline.dat}
#1 2 3
-0.38 0.01 500
-0.38 0.296 500
-0.32 0.298 500
-0.217 0.299 500
-0.017 0.149 500
-0.009 0.157 500
-0.006 0.127 500
-0.002 0.093 500
-0.001 0.069 500
-0.002 0.056 500
-0.38 0.01 500
\end{filecontents*}
\begin{filecontents*}{Rand.dat}
#1 2 3
0.025 -0.025 500
0.025 0.325 500
-0.425 0.325 500
-0.425 -0.025 500
0.025 -0.025 500
\end{filecontents*}

\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}
\begin{axis}[colorbar,
xmin=-0.45,
xmax=0.05,
ymin=-0.05,
ymax=0.35,
view={0}{90},
]
\begin{pgfonlayer}{main}
\addplot [no markers, very thick,save path=\pathA] table []{Outline.dat}
\closedcycle;
\end{pgfonlayer}
\begin{pgfonlayer}{pre main}
\clip [use path=\pathA];
\addplot3 [surf] gnuplot [raw gnuplot] {
set dgrid3d 40,40 spline;
splot 'Points.dat';
};
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}
\end{document}


• Works ! Thank you very much for this fast answer. Furthermore do you know how to add some lines in the plot to show the different areas? Like a contour plot ... Nov 4, 2018 at 19:52
• @C.Leopold Yes, there is the possibility to draw contour plots with pgfplots, see section 4.6.8 Contour Plots of the pgfplots manual. If you have problems with these, please ask a separate question. Asking questions is free (and I also do not want to dilute the current answer with additional, not immediately related things). If you feel that this answer solves your problem, could you perhaps consider accepting it by clicking on the check mark left of it?
– user121799
Nov 4, 2018 at 19:55