9

I am trying to construct a plot of a surface in 3d of which the color of the surface is dependent on the z coordinate. The difficult part is that I need the color to vary 'discretely', not continuously. I believe they call these plots something like iso or level surfaces, if I'm not mistaken.

Examples:

For a color varying continuously with the z coordinate, the construction in PGFplots is easy. The \addplot3[surf,shader=faceted interp] ... works just fine for this.

A minimum working example is

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot3
            [
                surf,
                domain=-1:1,
                y domain=-1:1,
                samples=50, % DECREASE FOR SHORTER COMPILATION.
                shader=faceted interp,
                colormap name={hot}
            ] {exp(-10*(x^2+y^2))};
        \end{axis}
    \end{tikzpicture}
\end{document}

which results in . How should I modify this LaTeX code to get discrete color variation like in the first two pictures?

3
  • 2
    You can define a colormap like the one given in tex.stackexchange.com/a/232710/2552
    – Jake
    Mar 17, 2015 at 13:01
  • 1
    @Jake. Works great! Do you want to write the answer to the question (wouldn't want to 'steal' your credits) or shall I do it for you? I can add a nice picture of the result.
    – Adriaan
    Mar 17, 2015 at 13:23
  • by all means, please go ahead! I look forward to seeing the result
    – Jake
    Mar 17, 2015 at 13:43

1 Answer 1

6

Conform the suggestion of Jake, defining a 'discrete' colormap, one can construct the desired iso / level surface.

The code in question,

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset
{
    colormap={redgreenblue}%
    {
        [2pt] % colormap steps: 2pt
        % blue: from 0000 to 1000
        rgb(0000pt)=(0,0,1);
        rgb(1000pt)=(0,0,1);
        % green:  from 1000 to 2000
        rgb(1000pt)=(0,1,0);
        rgb(2000pt)=(0,1,0);
        % red: from 2000 to 3000
        rgb(2000pt)=(1,0,0);
        rgb(3000pt)=(1,0,0);
    }
}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot3
            [
                surf,
                domain=-1:1,
                y domain=-1:1,
                samples=50, % DECREASE FOR SHORTER COMPILATION.
                shader=faceted interp,
                %colormap name={hot}
            ] {exp(-10*(x^2+y^2))};
        \end{axis}
    \end{tikzpicture}
\end{document}

results in

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .