14

I am drawing a 3D surface from external data. Here they are:

0 0 -0.64
1 0 -0.5
2 0 -0.4
3 0 -0.31
4 0 -0.23
5 0 -0.2
6 0 -0.24
7 0 -0.32
8 0 -0.42
9 0 -0.59
10 0 -0.76

0 1 -0.54
1 1 -0.4
2 1 -0.27
3 1 -0.17
4 1 -0.08
5 1 -0.03
6 1 -0.05
7 1 -0.14
8 1 -0.27
9 1 -0.45
10 1 -0.63

0 2 -0.46
1 2 -0.31
2 2 -0.16
3 2 -0.03
4 2 0.07
5 2 0.12
6 2 0.11
7 2 0.02
8 2 -0.13
9 2 -0.32
10 2 -0.51

0 3 -0.44
1 3 -0.26
2 3 -0.08
3 3 0.07
4 3 0.18
5 3 0.25
6 3 0.24
7 3 0.14
8 3 -0.01
9 3 -0.2
10 3 -0.41

0 4 -0.44
1 4 -0.26
2 4 -0.04
3 4 0.13
4 4 0.25
5 4 0.32
6 4 0.31
7 4 0.22
8 4 0.07
9 4 -0.14
10 4 -0.37

0 5 -0.44
1 5 -0.26
2 5 -0.05
3 5 0.14
4 5 0.27
5 5 0.34
6 5 0.33
7 5 0.24
8 5 0.09
9 5 -0.12
10 5 -0.36

0 6 -0.48
1 6 -0.29
2 6 -0.08
3 6 0.09
4 6 0.22
5 6 0.29
6 6 0.28
7 6 0.2
8 6 0.06
9 6 -0.13
10 6 -0.35

0 7 -0.55
1 7 -0.34
2 7 -0.15
3 7 -0.0
4 7 0.11
5 7 0.18
6 7 0.18
7 7 0.11
8 7 -0.02
9 7 -0.2
10 7 -0.39

0 8 -0.63
1 8 -0.44
2 8 -0.27
3 8 -0.13
4 8 -0.03
5 8 0.04
6 8 0.04
7 8 -0.02
8 8 -0.13
9 8 -0.29
10 8 -0.45

0 9 -0.71
1 9 -0.59
2 9 -0.43
3 9 -0.28
4 9 -0.18
5 9 -0.13
6 9 -0.13
7 9 -0.17
8 9 -0.26
9 9 -0.38
10 9 -0.53

0 10 -0.83
1 10 -0.75
2 10 -0.59
3 10 -0.45
4 10 -0.35
5 10 -0.3
6 10 -0.3
7 10 -0.34
8 10 -0.42
9 10 -0.52
10 10 -0.66

I am using the following code to plot it:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[view={-20}{20}, grid=both]
      \addplot3[surf] file {filename.txt};
    \end{axis}
  \end{tikzpicture}
\end{document}

You can see the result bellow: Result of the code:

I want to know how I can add a 2D surface on top of this like this question. So the only difference I feel is that I have external data instead of a function.

Thank you in advance.

makhlaghi
  • 1,882
  • May I ask you how you have generated the values for the plot? I think it is not out of head? – Dave Jan 19 '19 at 01:25

1 Answers1

11

The approach for adding a flat surface with the colour representing a data column is quite similar to that in the question you linked to.

You have to set the z value of the plot to a constant value, and use the meta value to represent the colour information. For this, you have to use the table plot style (instead of file), you need to set point meta=explicit in the \addplot options so PGFPlots knows not to use the z value for the meta value, and you have to put z expr=<come constant value>, meta index=2:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, filecontents}

\begin{filecontents*}{filename.txt}
0 0 -0.64
1 0 -0.5
2 0 -0.4
3 0 -0.31
4 0 -0.23
5 0 -0.2
6 0 -0.24
7 0 -0.32
8 0 -0.42
9 0 -0.59
10 0 -0.76

0 1 -0.54
1 1 -0.4
2 1 -0.27
3 1 -0.17
4 1 -0.08
5 1 -0.03
6 1 -0.05
7 1 -0.14
8 1 -0.27
9 1 -0.45
10 1 -0.63

0 2 -0.46
1 2 -0.31
2 2 -0.16
3 2 -0.03
4 2 0.07
5 2 0.12
6 2 0.11
7 2 0.02
8 2 -0.13
9 2 -0.32
10 2 -0.51

0 3 -0.44
1 3 -0.26
2 3 -0.08
3 3 0.07
4 3 0.18
5 3 0.25
6 3 0.24
7 3 0.14
8 3 -0.01
9 3 -0.2
10 3 -0.41

0 4 -0.44
1 4 -0.26
2 4 -0.04
3 4 0.13
4 4 0.25
5 4 0.32
6 4 0.31
7 4 0.22
8 4 0.07
9 4 -0.14
10 4 -0.37

0 5 -0.44
1 5 -0.26
2 5 -0.05
3 5 0.14
4 5 0.27
5 5 0.34
6 5 0.33
7 5 0.24
8 5 0.09
9 5 -0.12
10 5 -0.36

0 6 -0.48
1 6 -0.29
2 6 -0.08
3 6 0.09
4 6 0.22
5 6 0.29
6 6 0.28
7 6 0.2
8 6 0.06
9 6 -0.13
10 6 -0.35

0 7 -0.55
1 7 -0.34
2 7 -0.15
3 7 -0.0
4 7 0.11
5 7 0.18
6 7 0.18
7 7 0.11
8 7 -0.02
9 7 -0.2
10 7 -0.39

0 8 -0.63
1 8 -0.44
2 8 -0.27
3 8 -0.13
4 8 -0.03
5 8 0.04
6 8 0.04
7 8 -0.02
8 8 -0.13
9 8 -0.29
10 8 -0.45

0 9 -0.71
1 9 -0.59
2 9 -0.43
3 9 -0.28
4 9 -0.18
5 9 -0.13
6 9 -0.13
7 9 -0.17
8 9 -0.26
9 9 -0.38
10 9 -0.53

0 10 -0.83
1 10 -0.75
2 10 -0.59
3 10 -0.45
4 10 -0.35
5 10 -0.3
6 10 -0.3
7 10 -0.34
8 10 -0.42
9 10 -0.52
10 10 -0.66
\end{filecontents*}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[view={-20}{20}, grid=both]
      \addplot3[surf] file {filename.txt};
      \addplot3[surf, point meta=explicit] table [z expr=0.5, meta index=2] {filename.txt};
    \end{axis}
  \end{tikzpicture}
\end{document}
Jake
  • 232,450
  • How to know if tikz/pgfplots is using gnuplot from the .tex source ? when do we use explicit gnuplot commands like in PGFPLOTS ?. – texenthusiast Mar 18 '13 at 08:39
  • Thank you very much for the excellent explanation! It is only 3 days I am using pgfplots and I have a lot to learn. Thank you. – makhlaghi Mar 18 '13 at 08:39
  • @texenthusiast: gnuplot is only used if the gnuplot plot handler is used (\addplot gnuplot {...};). I don't understand the second question in your comment: This is PGFPlots, and gnuplot commands are only used when the gnuplot keyword is used. – Jake Mar 18 '13 at 08:43
  • @Jake In TeXample.net example there is no \addplot gnuplot{} handler, but it says gnuplot required. How to confirm whether .tex source needs gnuplot ? – texenthusiast Mar 18 '13 at 08:48
  • @Jake my second question is: when do we use explicitly \addplot gnuplot{} commands vs pgfmath engine? – texenthusiast Mar 18 '13 at 08:55
  • 1
    @texenthusiast: That example uses \addplot function, which is an alias for \addplot gnuplot. So whenever there's either \addplot function or \addplot gnuplot, the gnuplot backend is used. It's necessary to use gnuplot if the function you're trying to plot is too complicated for pgfmath. – Jake Mar 18 '13 at 09:20
  • @Jake Thanks a lot. Should we use Tikz or pgfplots to call gnuplot ? Which is better for larger data ? Is downsampling for large dataset done via gnuplot and on what basis ? – texenthusiast Mar 18 '13 at 09:27
  • 1
    @texenthusiast: Whether you use TikZ of PGFPlots to call gnuplot depends on whether you want to use PGFPlots' features to generate the axes, legend, labels and colour bars. For large data, gnuplot is better than pgfmath, but it doesn't matter much whether you use plain TikZ or PGFPlots. Downsampling can be done using gnuplot but it doesn't happen automatically. See pgfplot: plotting a large dataset for an example of how to downsample a large dataset. – Jake Mar 18 '13 at 09:37
  • @Jake Thanks a lot for all your comments. Last Question : if i had to submit my .tex to journal editor(where if gnuplot may not be available), is there any possibility to strip out (tikzpicture environment) to get annotated-figure.pdf with pdflatex -shell-escape runs. Does standalone does it ? – texenthusiast Mar 18 '13 at 09:51
  • 1
    @texenthusiast: You can simply use the external library that ships with TikZ: It creates separate pdf files for all your tikzpicture environments that can then simply be embedded using \includegraphics – Jake Mar 18 '13 at 10:02