7

I would like to draw a 2D, colored plot of a function above its surface plot.

I tried copying the surface plot and setting z filter/.code={\def\pgfmathresult{1.4}}, but then everything is the same color on the 2D plot.

Also, I'd be glad if I could avoid the antialiasing artifacts on the 2D plot (each quad is made of two triangles, and there is a small white line between them).

Surface plot below, 2D plot above

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[grid=both]
    \addplot3[
    surf,
    shader=faceted interp,
    samples=10,
    ] {sin(deg(x))*sin(deg(y))};

    \addplot3[
    surf,
    shader=faceted interp,
    samples=10,
    z filter/.code={\def\pgfmathresult{1.4}}
    ] {sin(deg(x))*sin(deg(y))};
  \end{axis}
\end{tikzpicture}
\end{document}
Suzanne Soy
  • 3,043
  • 1
    Related : pgfplots: color a surf using arbitrary colors. I found it after the answer was posted here, while searching for something totally different :) . – Suzanne Soy Feb 09 '13 at 22:21
  • The triangulation (quad->triangles) is actually a bug in some older version of pgfplots, not some kind of viewing artifact. It was (only) present for shader=faceted interp. It has been fixed in the meantime; an upgrade to the current stable version will eliminate it. – Christian Feuersänger Feb 11 '13 at 07:01
  • 1
    Just a side note: highly oscillating functions like yours involve a relatively high resolution in order to look smooth. A potentially interesting alternative is to use \usepgfplotslibrary{patchplots} and add the two options patch type=bicubic, patch type sampling, to your surface plot. This will cause a much smoother surface. Works only with plot-by-expression, though. – Christian Feuersänger Feb 11 '13 at 07:10

1 Answers1

9

The color of the plot depends on the meta value, which by default, is the z value in 3D plots. You can set it to something else, however: In this case, you'd say point meta={sin(deg(x))*sin(deg(y))} (so you'd use the original function for the meta value), and simply plot {1.4} instead of the function:

About the artifacts: That's probably a viewer issue. I don't see them using Adobe Reader or Evince on Ubuntu Linux.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[grid=both]
    \addplot3[
    surf,
    shader=faceted interp,
    samples=10,
    ] {sin(deg(x))*sin(deg(y))};

    \addplot3[
    surf,
    shader=faceted interp,
    samples=10,point meta rel=per plot,
    point meta={sin(deg(x))*sin(deg(y))}
    ] {1.4};
  \end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • My viewer is also evince, under ubuntu. Oh well… Could you please post the code for your answer? – Suzanne Soy Feb 09 '13 at 22:13
  • @GeorgesDupéron: Sorry, forgot about that. Question is edited. – Jake Feb 09 '13 at 22:14
  • @Jake, I'm getting errors: pgfkeys: Choice '1.7' unknown in key '/pgfplots/compat/anchors'. I am going to ignore this key. \pgfplotsset{compat=1.7} and others. – Sigur Feb 09 '13 at 22:50
  • 1
    @Sigur: Then you're probably using a version of PGFPlots older than 1.7. Just use compat=newest. – Jake Feb 10 '13 at 09:31
  • @Jake, one less error but now the error is pgfkeys: Choice 'faceted interp' unknown in key '/pgfplots/shader'. I am going to ignore this key. ...amples=10,] {sin(deg(x))*sin(deg(y))}; I guess that I should try to install pfgplots again. – Sigur Feb 10 '13 at 10:39
  • 1
    @Sigur otherwise just use one of the following for the shader= key : flat, flat corner, interp, facetted, facetted interp (There are a few more I think, check the documentation), or just remove that key. facetted gives the "lines" you see between the pathces, and interp makes the patches colored by a gradient, instead of having a single color. But yes, you should upgrade your pgfplots package :) . – Suzanne Soy Feb 11 '13 at 12:55
  • I am new to pgfplots so please excuse me if this question is very simplistic! I just wanted to know that If instead of a function I have point values (in a separate file) how can I add the 2D surface on top? I have already successfully drawn my 3D plot data, but I would like to add a 2D surface like the samples here. I would really appreciate any help. What part of this code has to be changed to what? – makhlaghi Mar 18 '13 at 07:53
  • @astroboy: You should open a new question for this that includes a small example document demonstrating how you plot the 3D surface. – Jake Mar 18 '13 at 07:54
  • Thanks for the advice, the question is posted in this address: http://tex.stackexchange.com/questions/103023/2d-surface-on-a-3d-surface-plot-external-data-in-a-file – makhlaghi Mar 18 '13 at 08:12