3

I want to make PGFPlots surf with my colormap rainbow. The funny thing is that I get error

I do not know the key '/pgfplots/colormap/rainbow' and I am going to ignore it. Perhaps you misspelled it.

and despite the error, my colormap is actually used! I guess there is some syntax problem, but I am not able to find it out. Please, help.

MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{surfdata.dat}
 0 0 0.8
 1 0 0.56
 2 0 0.5
 3 0 0.75

 0 2 0.6
 1 2 0.3
 2 2 0.21
 3 2 0.3

 0 4 0.68
 1 4 0.22
 2 4 0.25
 3 4 0.4

 0 6 0.7
 1 6 0.5
 2 6 0.58
 3 6 0.9
\end{filecontents}

\pgfplotsset{
/pgfplots/colormap={rainbow}{rgb255(0cm)=(96,0,96) rgb255(1cm)=(0,0,192) rgb255(2cm)=(0,160,160) rgb255(3cm)=(96,255,0) rgb255(4cm)=(255,255,0) rgb255(5cm)=(255,192,0) rgb255(6cm)=(255,0,0)}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[view={0}{90}]
  \addplot3[surf,colormap/rainbow] file {surfdata.dat};
\end{axis}
\end{tikzpicture}
  • 1
    Use \pgfplotsset{colormap/rainbow/.style={colormap={rainbow}{rgb....}}} – percusse Nov 14 '15 at 9:32
  • @percusse Thanks. So what's the point of name in colormap={name}{rgb...}? – Pygmalion Nov 14 '15 at 11:07
  • There is one additional layer of parsing the options at the axis setup. The style is for letting it choose the right context the name is used for the lower level as far as I understand (I have to admit I'm not uptodate lately with TeX related issues due to work). A design choice that only Christian Feuersanger can explain. – percusse Nov 14 '15 at 11:15
  • 1
    And use viridis :P tex.stackexchange.com/questions/228197/… – percusse Nov 14 '15 at 11:16
1

You almost did it right. For more details please have a look at the comments in the code.

% used PGFPlots v1.14
    \begin{filecontents}{surfdata.dat}
        0 0 0.8
        1 0 0.56
        2 0 0.5
        3 0 0.75

        0 2 0.6
        1 2 0.3
        2 2 0.21
        3 2 0.3

        0 4 0.68
        1 4 0.22
        2 4 0.25
        3 4 0.4

        0 6 0.7
        1 6 0.5
        2 6 0.58
        3 6 0.9
    \end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
    % by defining the colormap, it will also immideatly be loaded ...
    colormap={rainbow}{
        rgb255(0cm)=(96,0,96)
        rgb255(1cm)=(0,0,192)
        rgb255(2cm)=(0,160,160)
        rgb255(3cm)=(96,255,0)
        rgb255(4cm)=(255,255,0)
        rgb255(5cm)=(255,192,0)
        rgb255(6cm)=(255,0,0)
    },
}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            view={0}{90},
        ]
            \addplot3 [
                surf,
%                % ... that is, why you don't need to provide it here (again)
%                % but this would also be the wrong syntax
%                colormap/rainbow
            ] file {surfdata.dat};
        \end{axis}
    \end{tikzpicture}
    \begin{tikzpicture}
        \begin{axis}[
            view={0}{90},
            % right would either be calling the defined colormap here as ...
            % colormap name=viridis,
        ]
            \addplot3 [
                surf,
                % ... or here (with the same syntax)
                colormap name=hot,
            ] file {surfdata.dat};
        \end{axis}
    \end{tikzpicture}
        % change the default used colormap to `hot'
        \pgfplotsset{colormap name=hot}%
    \begin{tikzpicture}
        \begin{axis}[
            view={0}{90},
            % using "your" syntax is for calling predefined styles
            % which incorporate the defined colormaps.
            % Thus, if failed in your case, because you didn't define a style,
            % but just a colorbar ...
            %
            % This now just *defines* the style, but does not apply it so far
            colormap/rainbow/.style={
                colormap name=rainbow,
            },
%            % uncomment the next line to apply the previously defined style
%            colormap/rainbow,
        ]
            \addplot3 [surf] file {surfdata.dat};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.