[Tex/LaTex] Custom colorbar not showing colors correctly

pgfplotstikz-pgf

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

Best Answer

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