# How to use colors from a pgfplots colormap in own draw

Let's consider a set of ellipses (or say a signal), here 3, but in general that might be a huge array of 64x64

\documentclass{standalone}
\usepackage{tikz,pgfplots}

\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\draw[rotate around={-20:(0,0)},black] (0,0) ellipse (.5 and .25);
\draw[rotate around={0:(1,0)},black] (1,0) ellipse (.45 and .3);
\draw[rotate around={20:(2,0)},black] (2,0) ellipse (.4 and .35);
\end{tikzpicture}
\end{document}


And I further have values from 0 to 1 for each ellipse indicating a color, that is i would like to replace the black by some color from a colormap, maybe the /pgfplots/colormaps/hue colormap. Let these values for example be

 {0.2, 0.6, 0.3}


The I would like to read the colormap values at these points and use them to fill (or draw) the ellipses.

So for short the question is: How can I access a specific color in a pgfplots-colormap?

• After rereading your question and comments to answers I think you want to use colors declared by a combination of three values between 0 and 1. And this combination can produce different results based on something we call a colormap. Is this correct? – Ignasi Nov 5 '15 at 9:28
• By the way, where is pgfplots/colormaps/hue declared? I don't find it in pgfplots documentation. – Ignasi Nov 5 '15 at 9:31
• It's the second one on page 367 of the manual. – Ronny Nov 5 '15 at 14:49
• Does PGFPlots: point meta colormap index as fill value help you? – Ignasi Nov 10 '15 at 8:49
• Thanks a lot @Ignasi, that was exactly what I was looking for, see my answer. I think its advantage is the global style definition. – Ronny Nov 10 '15 at 13:53

As with the release of PGFPlots v1.13 you can use the new keys color of colormap or index of colormap to get easy access to the colors of the colormap. See section 4.7.6 pages 192f in the manual.

Here your code again using the first mentioned feature

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\pgfplotsset{compat=1.11}
\tikzset{
ellC/.style={
color of colormap={#1},
draw=.!80!black,
fill=.!80!white,
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
hide axis,
colormap/hsv,
xmin=-2.5, xmax=2.5,
ymin=-.5, ymax=.5,
axis equal,
]
\draw[ellC=200, rotate around={-20:(0,0)}] (0,0) ellipse (.5  and .25);
\draw[ellC=600, rotate around={0:(1,0)}]   (1,0) ellipse (.45 and .3);
\draw[ellC=300, rotate around={20:(2,0)}]  (2,0) ellipse (.4  and .35);
\end{axis}
\end{tikzpicture}
\end{document}


• Shouldn't compat be set to 1.13, given the description? – Rmano Jan 10 '16 at 8:45
• @Rmano: No, compat is only needed for compatibility reasons, i.e. an already existing feature has a changed behavior at that release. The above example would also have worked with compat=1.11, because with this release there is no need to add axis cs: to the coordinates when using TikZ commands like \draw. I changed the value accordingly. – Stefan Pinnow Jan 10 '16 at 12:27
• Interesting! What's the scaling here? Does it still use colors from 0 to 1000? Or are nonintegers also allowed? Also the .!80!black is new for me. Nice style! – Ronny Jan 10 '16 at 17:10
• @Ronny: color of colormap makes an interpolation in the range [0,1000] and accepts floating point numbers. The . is a magic color name reflecting the "current color" and thus can be used as any other color like red. – Stefan Pinnow Jan 10 '16 at 19:37

Something like this?

I've used PGFPlots \addplot to make possible the use of \pgfplotscolormapdefinemappedcolor for color mapping. Maybe this is not a fancy solution, I think there must be a better.

# Code

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[hide axis,
colormap/hot,
xmin=-2.5, xmax=2.5,
ymin=-2.5, ymax=2.5,
domain=0:360, samples=61, thick]
mapped color, variable=\t,rotate around={-20:(0,0)}] ({.5*cos(t)},{.25*sin(t)});
mapped color, variable=\t] ({.45*cos(t)+1},{.3*sin(t)});
mapped color, variable=\t,rotate around={20:(2,0)}] ({.4*cos(t)+2},{.35*sin(t)});
\end{axis}
\end{tikzpicture}
\end{document}


# Update

The argument of the macro \pgfplotscolormapdefinemappedcolor{<number>} is a <number> into the range 0-1000.

I didn't find a way to use this \pgfplotscolormapdefinemappedcolor approach into TikZ drawing commands, neither into TikZ drawing commands inside the axis environment, sorry! If you use it there is no error but color is always black. If you add fill=mapped color!50 you can use the mapped color to fill each ellipse.

Probably you can setup a macro to draw every ellipse into the array using a fancy loop. ;)

• The result looks nice, though the code is a little lenthy, you're right ;). Can you extend your answer with respect to a few points: What's the total range of the argument of \pgfplotscolormapdefinemappedcolor? 0-1000? And would it be possible to use the color for drawas you did and let's say the !50 of that color for fill=? – Ronny Nov 4 '15 at 5:53
• I don't think you need to use a PGFPlot colormap to do this thing. ;) – OSjerick Nov 4 '15 at 11:58
• As you can see in the other answer it seems you don't, but there are so many nice ones predefines in pgfplots... – Ronny Nov 4 '15 at 19:44

If you just want to define a series of colors which can be used referenced by number, you can declare a colorseries from xcolor package

\documentclass[tikz,border=2mm]{standalone}

\begin{document}

\definecolorseries{test}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
\resetcolorseries{test}

\begin{tikzpicture}

\foreach \i [count=\ni from 0] in {1,...,64}
\node[fill={test!![\ni]}, minimum size=1cm] at ({mod(\ni,8)},{int(\ni/8)}) {\ni};

\end{tikzpicture}

\end{document}


Applied to your example, could be:

\begin{tikzpicture}
\draw[rotate around={-20:(0,0)},{test!![1]}, fill={test!![1]!50}] (0,0) ellipse (.5 and .25);
\draw[rotate around={0:(1,0)},{test!![2]}, fill={test!![2]!50}] (1,0) ellipse (.45 and .3);
\draw[rotate around={20:(2,0)},{test!![3]}, fill={test!![3]!50}] (2,0) ellipse (.4 and .35);
\end{tikzpicture}


• oh, that's also neat, however, my color is actually computed to be from [0,1] on the hue colormap, so your nice color series code would have to be a sampled colormap, i.e. the hue. – Ronny Nov 4 '15 at 16:00
• @Ronny: Please take a look at xcolor documentation. It explains how these colorseries are defined/constructued. My example is just this, an example to present an alternative solution. – Ignasi Nov 4 '15 at 16:35
• Thx, will look into that; I think you're alternative is a good idea and I will try it the next few days :) – Ronny Nov 4 '15 at 19:43
• @Ronny This answer is what you need. Cheers! – OSjerick Nov 4 '15 at 22:08
• Oh, if you say so too, i really take a look into defining the hue colormap with colorseries. – Ronny Nov 4 '15 at 22:19

While defining a colormap with xcolor like @Ignasi mentioned is a great idea and works fine, the colormaps of pgfplots have also many Matlab colormaps predefined, for example my favourite one, the hue colormap colormap/hsv. I extended an answer from this post, which was also brought to my attention by @Ignasi (so thanks alot for both answers). This yields code like this

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\pgfplotsset{compat=1.12}
\tikzset{ellC/.style={/utils/exec={\pgfplotscolormapdefinemappedcolor{#1}},%
draw=mapped color!80!black, fill=mapped color!80!white}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[hide axis,
colormap/hsv,
xmin=-2.5, xmax=2.5,
ymin=-.5, ymax=.5,
axis equal]
\draw[ellC=0, rotate around={-20:(0,0)}] (0,0) ellipse (.5 and .25);
\draw[ellC=50, rotate around={0:(1,0)}] (1,0) ellipse (.45 and .3);
\draw[ellC=100, rotate around={20:(2,0)}] (2,0) ellipse (.4 and .35);
\end{axis}
\end{tikzpicture}
\end{document}


which i think is as short as using the xcolor-style with !! and works fine (i.e. colors the first ellipse red and continues towards yellow). furthermore one can easily change the style of the ellipses (line 6) and the colormap (line 10)

Of course, this assumes (see linked post above) that the used colormap contains 1000 color values, like the predefined ones from pgfplots.

Inspired by other answers, but does not uses axis environment. I find it cleaner.

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.9}
\usetikzlibrary{calc,pgfplots.colormaps}
\begin{document}
\begin{tikzpicture}[% Defines the colormap
/pgfplots/colormap/hsv,
ellipse/.style={/utils/exec={
% Defines a color "mapped color"
\pgfplotscolormapdefinemappedcolor{#1}},
fill=mapped color}]
\draw[rotate around={-20:(0,0)},ellipse=200] (0,0) ellipse (.5 and .25);
\draw[rotate around={0:(1,0)},ellipse=300] (1,0) ellipse (.45 and .3);
\draw[rotate around={20:(2,0)},ellipse=400] (2,0) ellipse (.4 and .35);
\end{tikzpicture}
\end{document}


• Looks nice! Is it necessary to go as far back as 1.9 in compatibility or would it also just work with 1.14? – Ronny Nov 22 '16 at 5:11
• I have not tested it on 1.14. It should work. If you are using 1.14, it is better to use color of colormap or index of colormap as suggested by Stefan Pinnow. After all the function \pgfplotscolormapdefinemappedcolor{#1} is not actually documented in user guide, it has been picked from the source code. If you want to use it outside the axis environment, you may have to provide the full path of the key, something like /pgfplots/index of colormap=200. – Vikas Nov 22 '16 at 13:01
• you're right, these new commands are far more convenient to use. – Ronny Nov 22 '16 at 14:33