[Tex/LaTex] Specifying which user defined colormap to use in pgfplots

pgfplots

I have figured out how to define my own colormap in pgfplots. My understanding is that when I define my color map and I plot any graph that requires a colorbar, the default coloring scheme used becomes the one that I have defined. My question is, what if I am defining multiple colormaps, how do I specify the one I want to pick? I couldn't really find the options. Here is a MWE

 \documentclass{article}
     \usepackage{pgfplots}
     \usepackage{tikz}
     \pgfplotsset{
        colormap={mygreen}{rgb255(0cm)=(0,0,0); rgb255(1cm)=(0,255,0)}
         }
   \begin{document}
      \begin{figure}
        \begin{tikzpicture}[scale=0.45]
             \begin{axis}[view={0}{90},colorbar]
                \addplot3[surf, shader=interp] table{
              1      0     2
              2      0     1

              1      1     0
              2      1     3

              1      2     1
              2      2     1
             };
             \end{axis} 
         \end{tikzpicture}
      \end{figure}
   \end{document}

The above example will use mygreen colormap. Say for example I have defined another colormap colormap={randcolor}{rgb255(0cm)=(0,100,0); rgb255(1cm)=(100,255,155)}, how would I choose to use that one? At first I used the option colormap/mygreen, but it was giving me some compilation errors.

Best Answer

The option for this is called colormap name and can be set to your axis. Please see my example below:

% arara: pdflatex

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{%
    ,compat=1.13
    ,colormap={mygreen}{rgb255(0cm)=(0,0,0); rgb255(1cm)=(0,255,0)}
    ,colormap={myblue}{rgb255(0cm)=(0,0,0); rgb255(1cm)=(0,0,255)}
    ,colormap={myred}{rgb255(0cm)=(0,0,0); rgb255(1cm)=(255,0,0)}
}
\usepackage{filecontents} % this would be a good approach for your next MWE. A very handy package.
\begin{filecontents*}{somesurf.data}
    1      0     2
    2      0     1

    1      1     0
    2      1     3

    1      2     1
    2      2     1
\end{filecontents*}

\begin{document}
\begin{figure}
    \begin{tikzpicture}[scale=0.4] % reduced scale in order to fit three in a row
        \begin{axis}[view={0}{90},colorbar,colormap name=myred] % the option colormap name is set here
            \addplot3[surf, shader=interp] table{somesurf.data};
        \end{axis} 
    \end{tikzpicture}
    \begin{tikzpicture}[scale=0.4]
        \begin{axis}[view={0}{90},colorbar,colormap name=mygreen]
            \addplot3[surf, shader=interp] table{somesurf.data};
        \end{axis} 
    \end{tikzpicture}
    \begin{tikzpicture}[scale=0.4]
        \begin{axis}[view={0}{90},colorbar,colormap name=myblue]
            \addplot3[surf, shader=interp] table{somesurf.data};
        \end{axis} 
    \end{tikzpicture}
\end{figure}
\end{document}

enter image description here

Related Question