pgfplots sample colors from colorbar

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?

• @Jake I would like to have the color values and show a sampled color bar with a scale. I will add an example to the question. – Miguel de Val-Borro Jul 30 '13 at 21:17
• @Jake That's right, each plot has the same color in the whole domain. – Miguel de Val-Borro Jul 30 '13 at 21:41

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}{
execute at begin plot visualization={%
\pgfplotscolormapdefinemappedcolor{\numexpr#1*200-100\relax}
},
mapped color,
thick
] {#1*sin(x)};
}
\end{axis}
\end{tikzpicture}
\end{document}

• Thanks a lot. I get this error when I compile this example on Texlive 2012:! Undefined control sequence. \pgfplotscolormapdefinemappedcolor ...formedrange ] [1.0] {#1} {\pgfkeysvalu... – Miguel de Val-Borro Jul 30 '13 at 22:20
• @MigueldeVal-Borro: What version of PGFPlots are you using? – Jake Jul 30 '13 at 22:22
• pgfplots 2011/12/29 v1.5.1 – Miguel de Val-Borro Jul 30 '13 at 22:24
• @MigueldeVal-Borro: Ah, yes, that's pretty dated. I would recommend updating to the current version (1.8) – Jake Jul 30 '13 at 22:25
• Thanks, however the pgfplotscolormapdefinemappedcolor does not work for me when I read the data coordinates from a file like \addplot table[x index=0,y index=1] {datafile.dat}; – Miguel de Val-Borro Jul 31 '13 at 14:55