Creating a colormap in tikz/pgfplots, no axis

You can make the axis take up no space by setting scale only axis, width=0pt, height=0pt, hide axis.

To be able to set the range of the colorbar, you have to provide a dummy plot. Then you can set point meta min and point meta max to control the range:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    hide axis,
    scale only axis,
    height=0pt,
    width=0pt,
    colormap/jet,
    colorbar horizontal,
    point meta min=18,
    point meta max=45,
    colorbar style={
        width=10cm,
        xtick={18,20,25,...,45}
    }]
    \addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}

\end{document}

\pgfplotscolorbardrawstandalone[options] does exactly that. It seems it was created for illustrating the tikz docs:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\pgfplotscolorbardrawstandalone[ 
    colormap/jet,
    colorbar horizontal,
    point meta min=18,
    point meta max=45,
    colorbar style={
        width=10cm,
        xtick={18,20,25,...,45}}]
\end{tikzpicture}

\end{document}

The result is the same but it's much simpler to use and to implement!

enter image description here

Tags:

Tikz Pgf