2

Is it possible to define automatically total colors equally distributed in the color map below? I mean, for i from 1 to total I'd like to get a color c-i, where c-0 is the left most blue color and c-total is the right most red color.

I'd like to use this procedure to fill the circles in the MWE below.

enter image description here

MWE

\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\def\total{10}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,...,\total}{
\draw[fill] (\x,0) circle (.5cm);
%\draw[fill=<color should be here, depending on \x>] (\x,0) circle (.5cm);
}
\end{tikzpicture}
\end{document}
  • Use the HSB model? – Symbol 1 Apr 1 at 1:27
  • @Symbol1, could you elaborate? – Sigur Apr 1 at 1:30
4

You can use the wavelength color model

\documentclass[tikz,border=9]{standalone}

\begin{document}
    \def\total{10}
    \begin{tikzpicture}
    \foreach \x in {0,...,\total}{
        \pgfmathsetmacro\wavelen{400+(700-400)*\x/\total}
        \definecolor{ad hoc color}{wave}{\wavelen}
        \fill[ad hoc color](\x,0)circle(.5cm);
    }
    \end{tikzpicture}
\end{document}

| improve this answer | |
  • Strange, I'm not using the class like you (\documentclass[tikz,border=9]{standalone}). I'm loading tikz after. When I tried to fill using the color, I get Package pgf Error: Unsupported color model hsb'.` – Sigur Apr 1 at 1:43
  • That is indeed weird. Maybe double check with the xcolor manual. It could be possible that TikZ/xcolor loads a different driver that does not support wave. – Symbol 1 Apr 1 at 1:45
  • Hum, something strange in my complex drawing code. Your MWE works. Let me check. – Sigur Apr 1 at 1:46
  • Curious. \draw[fill={ad hoc color}] does not work. Neither \draw[fill=ad hoc color]. But \fill[ad hoc color,draw=black](\x,0)circle(.5cm); does. – Sigur Apr 1 at 2:07
  • That, I think, is interesting enough to become a new question. Bother/Consider asking? – Symbol 1 Apr 1 at 17:40
4

pgfplots has this in color of colormap, see p. 211 of the manual

enter image description here

There are tons of colormaps, and each of them can be accessed with an index that runs from 0 to 1000.

\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\def\total{10}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,...,\total}{
\fill[/pgfplots/color of colormap=\x*100 of hot] (\x,0) circle[radius=.5cm];
}
\end{tikzpicture}
\end{document}

enter image description here

Note that many nice options are collected in section 5.2 ColorBrewer of pgfplots manual v1.16.

The colormap that seems to come close to your screen shot is called jet. Here are two series of circles, the upper ones are discrete steps, whereas the lower ones have a continuous color transition.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{/pgfplots/colormap={jet}{rgb255(0cm)=(0,0,128) rgb255(1cm)=(0,0,255)
rgb255(3cm)=(0,255,255) rgb255(5cm)=(255,255,0) rgb255(7cm)=(255,0,0)
rgb255(8cm)=(128,0,0)}}
\begin{document}
\begin{tikzpicture}
\begin{scope}[scale=1.55,xshift=0.5cm]
\pgfplotscolorbardrawstandalone[,
    colormap={myjet}{
        samples of colormap=(8 of jet)
    },
    colorbar horizontal,
    colormap access=map,
    xticklabel style={opacity=0,overlay}
]
\end{scope}
\foreach \x in {1,...,10}{
\fill[/pgfplots/color of colormap=\x*100 of jet] (\x,-2.25) circle[radius=.5cm];
}

\path[clip] foreach \X in {1,...,10}{ (\X,-5)circle[radius=.5cm]};
\begin{scope}[yshift=-3.6cm,xscale=1.8,yscale=2.5]
\pgfplotscolorbardrawstandalone[,
    colormap={myjet}{
        samples of colormap=(8 of jet)
    },
    colorbar horizontal,
    colormap access=map,
    xticklabel style={opacity=0,overlay}
]
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

The lower ones are somewhat harder to obtain with TikZ "only" (to the best of my knowledge).

| improve this answer | |
  • Perfect. I didn't know about /pgfplots/color of colormap=. – Sigur Apr 1 at 1:35
  • @Sigur I thought the answer achieves something along those lines already. Anyway, I added some color map which seems to be closer to your screen shot. – Schrödinger's cat Apr 1 at 2:03
  • Thanks a lot. I'll see other color maps also. Maybe some with very distinct colors. But your solution teaches me a lot. – Sigur Apr 1 at 2:04
  • Maybe you want \pgfdeclarehorizontalshading. – Symbol 1 Apr 1 at 17:51
  • @Symbol1 Yes, that's also a possibility but to the best of my knowledge it does not come with a built-in color of colormap key. That is, even if you have declared the shading, you do not know what the color is at, say 12% of the full length. Of course, one can figure this out, but this is what pgfplots spares us from doing. – Schrödinger's cat Apr 1 at 18:07

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.