4

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.

  • Welcome to TeX.SX! You code does not compile. Please add some data with the package filecontents, correct the typo in color(r)map... Correct everything needed in order to give us a good start for copying and testing your issue. Thanks. – LaRiFaRi Jun 8 '15 at 7:16
  • 1
    @LaRiFaRi, thanks for the comment, I edited the code and I made sure it compiled. – ZanCoul Jun 8 '15 at 7:24
4

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

  • Wonderful! Exactly what I was looking for. I somehow missed that in the pgfplots manual. Thanks agian. – ZanCoul Jun 8 '15 at 7:39
  • 1
    @ZanCoul Glad that I could help. Sometime one misses the most obvious things... :-) This option is the very first line of the chapter "4.7.6 Color Maps". – LaRiFaRi Jun 8 '15 at 7:43

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.