[Tex/LaTex] pgfplots sample colors from colorbar

colorpgfplotstikz-pgf

I would like to get the color definition from a sample of colors in a pgfplots colormap using for example the RGB model. When I choose colormap/bluered with colorbar style={samples=10} I need to have the xcolor definition of those particular colors to plot different curves. Is there an automatic way to generate these color specifications from the colormap definition?

Best Answer

You can make a colour from the colormap available as mapped color by calling the macro \pgfplotscolormapdefinemappedcolor{<value>}, where <value> is a number between 0 and 1000 that is mapped linearly into the current color map.

In order to use different colours for different plots, this macro needs to be called within execute at begin plot visualization, otherwise all the plots end up with the same colour:

\documentclass[border=1mm]{standalone}
\usepackage{pgfplots}
%\usepgfplotslibrary{colormaps}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    colormap/hot,
    domain=0:360,
    colorbar sampled,
    colorbar style={samples=6},
    point meta min=0,
    point meta max=10
]

\pgfplotsinvokeforeach{1,...,5}{
\addplot [
    execute at begin plot visualization={%
        \pgfplotscolormapdefinemappedcolor{\numexpr#1*200-100\relax}
    },
    mapped color,
    thick
] {#1*sin(x)};
}
\end{axis}
\end{tikzpicture}
\end{document}
Related Question