[Tex/LaTex] ColorBar: extend, custom colormap

pgfplotstikz-pgf

I would like to create a custom colorbar, at the moment I have the following code:

\documentclass[tikz,border={12pt,12pt}]{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    hide axis, scale only axis, height=0pt, width=0pt, % hide axis
    colormap/jet,
    colorbar sampled,
    colorbar horizontal,
    point meta min = 20,
    point meta max = 40,
    colorbar style = {
        samples = 10,
        height = 0.5cm,
        width = 10cm,
        xticklabel style = {
            text width = 2.5em,
            align = center,
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision = 1,
            /tikz/.cd
        }
    }
    ]
    \addplot [draw=none] coordinates {(0,0)};
    \end{axis}
\end{tikzpicture}
\end{document}

Current Status
Now I would like to:

  • add extend (upper/lower arrow) like the last colorbar of this example http://matplotlib.org/examples/api/colorbar_only.html
  • specify just point meta min, point meta max and samples so each colored zone and ticks are aligned without using xtick = {20,22.22,...,40}
  • remove the vertical dashed green line of each tick
  • use a custom colormap defined using RGB for example:
[ [191, 191, 191],  
  [255, 255, 0  ],  
  [255, 201, 0  ],  
  [255, 150, 0  ],  
  [255, 79 , 0  ],  
  [255, 25 , 0  ],  
  [229, 0  , 25 ],  
  [176, 0  , 79 ],  
  [105, 0  , 150],  
  [54 , 0  , 201],  
  [0  , 0  , 255],  
  [102, 102, 102] ]  

Thank you very much.

Best Answer

Here is the code which should address all of your needs including the comment and answer already given. For more details please have a look at the comments in the code.

% (used PGFPlots v1.14)
\documentclass[border={2pt}]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % define the custom colormap
        colormap={my colormap}{
            rgb255=(191, 191, 191),
            rgb255=(255, 255, 0  ),
            rgb255=(255, 201, 0  ),
            rgb255=(255, 150, 0  ),
            rgb255=(255, 79 , 0  ),
            rgb255=(255, 25 , 0  ),
            rgb255=(229, 0  , 25 ),
            rgb255=(176, 0  , 79 ),
            rgb255=(105, 0  , 150),
            rgb255=(54 , 0  , 201),
            rgb255=(0  , 0  , 255),
            rgb255=(102, 102, 102),
        },
    }
\begin{document}
    \begin{tikzpicture}
            % define `point meta min' and `point meta max' values ...
            \pgfmathsetmacro{\PointMetaMin}{20}
            \pgfmathsetmacro{\PointMetaMax}{40}
            % ... and calculate from that the `tick distance' value
            \pgfmathsetmacro{\XTickDistance}{
                (\PointMetaMin - \PointMetaMax)
                    /
                (\pgfplotscolormapsizeof{my colormap} - 2)
            }
        \begin{axis}[
            hide axis,
            scale only axis,
            height=0pt,
            width=0pt,
            %
            % use defined custom colormap
            colormap name=my colormap,
            %
            colorbar sampled,
            colorbar horizontal,
            point meta min=\PointMetaMin,
            point meta max=\PointMetaMax,
            colorbar style={
                samples=10,
                height=0.5cm,
                width=10cm,
                % don't draw ticks
                xtick style={
                    draw=none,
                },
                xticklabel style={
                    text width=2.5em,
                    align=center,
                    /pgf/number format/.cd,
                        fixed,
                        fixed zerofill,
                        precision=1,
                    /tikz/.cd,
                },
                % specify number ticks indirectly by calculating the distance
                % between the ticks
                xtick distance=\XTickDistance,
%                % (in case of some numerical issues it could be that the
%                %  min or max value isn't drawn; then add it "by hand")
%                extra x ticks={20},
            },
        ]
            \addplot [draw=none] coordinates {(0,0)};
        \end{axis}

        % ---------------------------------------------------------------------
        % code borrowed/copied from Salim Bou
        % <http://tex.stackexchange.com/a/332352/95441>
        \def\len{5mm}
        \definecolor{leftcolor}{RGB}{191, 191, 191}
        \definecolor{rightcolor}{RGB}{102, 102, 102}
        \foreach \i/\j in {
            south east/a,
            east/b,
            north east/c,
            north west/e,
            west/f,
            south west/g%
        }{
            \coordinate (\j) at (current colorbar axis.\i);
        }
        \filldraw [fill=leftcolor]  (a) -- ([xshift=\len] b) -- (c);
        \filldraw [fill=rightcolor] (e) -- ([xshift=-\len]f) -- (g);
        % ---------------------------------------------------------------------

    \end{tikzpicture}
\end{document}

image showing the result of above code