4

Does the pgfplots package have the option to adjust the width or height of the colorbar? For example, I want to make the colorbar in the following figure more narrow

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    view={0}{90},
    colormap/cool, 
    colorbar horizontal,
    colorbar style={
    at={(0,1.2)},
    anchor=north west,
    height=2cm,
    width=2cm,}
]
\addplot3 [surf, shader=flat] {x+y};
\end{axis}
\end{tikzpicture}
\end{document} 
  • I think you answered you question yourself, just change height or width in your colorbar style, or maybe I misunderstood your question. – BambOo Mar 27 '18 at 12:14
7

The right key to use is colorbar/width to change the width of the colorbar. To change the height use height in the colorbar style options or width in conjunction with colorbar horizontal.

(I also did some minor improvements for the positioning of the colorbar which is now independent of the chosen colorbar/width.)

For details please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        view={0}{90},
        colormap/cool,
        colorbar horizontal,
        colorbar style={
            at={(0,1.0)},               % <-- (changed)
            anchor=below south west,    % <-- (changed)
            % change the width of the colorbar relative to the main `axis' environment
            width=0.5*\pgfkeysvalueof{/pgfplots/parent axis width},
        },
        % because you are using `colorbar horizontal' the following key
        % now changes the `height' of the colorbar
        colorbar/width=2.5mm,
    ]
        \addplot3 [surf, shader=flat] {x+y};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

| improve this answer | |
  • Interesting. Thus, width inside colorbar style and colorbar/width outside of it modify different internal variables? – AlexG Aug 30 '19 at 6:41
  • 1
    I guess yes, but only when you are using colorbar horizontal. – Stefan Pinnow Aug 30 '19 at 9:08

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.