4

First time posting on SE, trying my best with formatting, behaviour and such.

I'm trying to create a stand-alone colorbar, to use next to a image. I want to have 10 discrete colors, that I define in my custom colormap. Why does not TikZ use these 10-rgb colors in the colorbar? It seems to interpolate in between and apply some shading.

I want it to look like this: enter image description here

But it ends up looking like this:enter image description here I have used MS Paint to find the RGB-colors of the colorbar. Why are they now showing as the RGB-colors I entered? My code is below.

\pgfplotsset{
    % define the custom colormap
    colormap={my colormap}{
        rgb255=(0,0,255),
        rgb255=(0, 100, 255),
        rgb255=(0, 160, 255),
        rgb255=(0, 210, 255),
        rgb255=(0, 255, 255),
        rgb255=(0, 255, 0),
        rgb255=(0, 255, 255),
        rgb255=(0, 255, 200),
        rgb255=(0, 255, 200),
        rgb255=(255, 0  ,0),
    },
}
\begin{tikzpicture}
\begin{axis}[
        hide axis,
        scale only axis,
        height=0pt,
        width=0pt,
        colormap name=my colormap,
        colorbar sampled,
        colorbar horizontal,
        point meta min=0,
        point meta max=1,
        colorbar style={
            xlabel={Relative density},
            samples=11,
            height=0.5cm,
            width=10cm,
            xtick style={
                color=black}
            },
    ]
        \addplot [draw=none] coordinates {(0,0)};
    \end{axis}
\end{tikzpicture}

Thanks, Johan

  • Welcome to TeX.SX! If you want someone to help you, you should post a complete minimal working example MWE beginning at \documentclass and ending with \end{document}, including all packages and commands necessary to compile your code and create the issue. – Georg May 21 '17 at 17:57
  • You aren't specifying the colour map correctly. – cfr May 21 '17 at 18:13
  • I understand. Where am I going wrong? – Johan May 22 '17 at 6:27
6

The RGB Values that you add are wrong. And you have to add colormap access=piecewise const otherwise the colour displayed is dependent on the colours defined before and after it.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\pgfplotsset{
    % define the custom colormap
    colormap={my colormap}{
        rgb255=(0, 0, 255),
        rgb255=(10, 100, 255),
        rgb255=(20, 160, 255),
        rgb255=(30, 210, 255),
        rgb255=(30, 255, 255),
        rgb255=(30, 255, 40),
        rgb255=(255, 255, 50),
        rgb255=(255, 200, 50),
        rgb255=(255, 120, 40),
        rgb255=(255, 0, 0),
    },
}
\begin{tikzpicture}
\begin{axis}[
        hide axis,
        scale only axis,
        height=0pt,
        width=0pt,
        colormap name=my colormap,
        colorbar sampled,
        colormap access=piecewise const, % add this
        colorbar horizontal,
        point meta min=0,
        point meta max=1,
        colorbar style={
            xlabel={Relative density},
            samples=11,
            height=0.5cm,
            width=10cm,
            xtick style={
                color=black}
            },
    ]
        \addplot [draw=none] coordinates {(0,0)};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here


Just to demonstrate it lets look at

\pgfplotsset{
    colormap={my colormap}{
        rgb255=(0, 0, 0),
        rgb255=(255, 255, 255),
    },
}

Where the first block should be black and the second white.

enter image description here

  • What was the mistake in the code of the OP? – Dr. Manuel Kuehner May 21 '17 at 18:02
  • 1
    @Dr.ManuelKuehner It was just the wrong RGB Values and nothing Tex related. – Georg May 21 '17 at 18:05
  • You can comment out the line colormap name=my colormap,. – cfr May 21 '17 at 18:17
  • Which seems odd. I guess defining it also activates it. The commas in the colormap specification don't seem to match the syntax in the manual. Or am I looking at the wrong case? (It just uses spaces for simple maps.) – cfr May 21 '17 at 18:24
  • The colorbar I want to achieve has its last cell completely red, i.e an RGB code of (255,0,0) which I added. Your last cell has (255,30,30), but even if I change it to what I want it's not the right shade of red. Do I misinterpret how a colormap works perhaps? My thinking is the 10 colors I define in my colormap will be the 10 I see in the colorbar. – Johan May 22 '17 at 6:26

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.