12

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?

2
  • @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.
    – gypaetus
    Jul 30 '13 at 21:17
  • @Jake That's right, each plot has the same color in the whole domain.
    – gypaetus
    Jul 30 '13 at 21:41
10

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}
8
  • Thanks a lot. I get this error when I compile this example on Texlive 2012:! Undefined control sequence. \pgfplotscolormapdefinemappedcolor ...formedrange ] [1.0] {#1} {\pgfkeysvalu...
    – gypaetus
    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
    – gypaetus
    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
  • 1
    The code of this answer does not seem to work anymore. Anyway, how can I make sure that the mapped color of every plot is also used for the corresponding legend entry? Aug 18 '17 at 20:15

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.