17

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?

5
  • 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
    Commented Nov 5, 2015 at 9:28
  • By the way, where is pgfplots/colormaps/hue declared? I don't find it in pgfplots documentation.
    – Ignasi
    Commented Nov 5, 2015 at 9:31
  • It's the second one on page 367 of the manual.
    – Ronny
    Commented Nov 5, 2015 at 14:49
  • 1
    Does PGFPlots: point meta colormap index as fill value help you?
    – Ignasi
    Commented Nov 10, 2015 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
    Commented Nov 10, 2015 at 13:53

5 Answers 5

12

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}

image of the result of above code

6
  • Shouldn't compat be set to 1.13, given the description?
    – Rmano
    Commented Jan 10, 2016 at 8:45
  • 1
    @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. Commented Jan 10, 2016 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
    Commented Jan 10, 2016 at 17:10
  • 2
    @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. Commented Jan 10, 2016 at 19:37
  • Which colormap is used here? How can I select for instance jet?
    – Matthias
    Commented Mar 18, 2021 at 17:44
6

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}

enter image description here

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}

enter image description here

5
  • 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
    Commented Nov 4, 2015 at 16:00
  • 1
    @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
    Commented Nov 4, 2015 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
    Commented Nov 4, 2015 at 19:43
  • @Ronny This answer is what you need. Cheers!
    – osjerick
    Commented Nov 4, 2015 at 22:08
  • Oh, if you say so too, i really take a look into defining the hue colormap with colorseries.
    – Ronny
    Commented Nov 4, 2015 at 22:19
5

Something like this? enter image description here

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]
            \addplot[execute at begin plot visualization={\pgfplotscolormapdefinemappedcolor{0}}, 
                     mapped color, variable=\t,rotate around={-20:(0,0)}] ({.5*cos(t)},{.25*sin(t)});
            \addplot[execute at begin plot visualization={\pgfplotscolormapdefinemappedcolor{300}}, 
                     mapped color, variable=\t] ({.45*cos(t)+1},{.3*sin(t)});
            \addplot[execute at begin plot visualization={\pgfplotscolormapdefinemappedcolor{600}}, 
                     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.

enter image description here

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

3
  • 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
    Commented Nov 4, 2015 at 5:53
  • I don't think you need to use a PGFPlot colormap to do this thing. ;)
    – osjerick
    Commented Nov 4, 2015 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
    Commented Nov 4, 2015 at 19:44
5

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)enter image description here

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

4

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}

TeX output

3
  • 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
    Commented Nov 22, 2016 at 5:11
  • 1
    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
    Commented Nov 22, 2016 at 13:01
  • you're right, these new commands are far more convenient to use.
    – Ronny
    Commented Nov 22, 2016 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .