# Alignment of colorbar with colorbar to name

I am trying to make a group of plots with groupplots sharing the same colorbar and the same y axis. An example:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[%
point meta min=0, point meta max=25,
group style={group size=2 by 1,
yticklabels at=edge left, ylabels at=edge left,
},
colorbar,
colorbar to name=commonbar,
]
\nextgroupplot
\nextgroupplot
\end{groupplot}
\end{tikzpicture}
\ref{commonbar}
\end{document}


Here, the colorbar to name option takes care of using the same colorbar for all plots (there is a warning from pdflatex on "multiply defined labels" -- to be expected, I suppose, and no problem as long as the range of the colorbar is specified). The colorbar is then "placed" by the \ref{commonbar} command.

How can I align the colorbar properly with the figures? I have tried placing the \ref command in several places (the placement above gives the best result I've seen so far). You may say I'm looking for the equivalent of yticklabels at=edge left for colorbars.

-

The warning about the multiply defined labels comes from the fact that you supplied the colorbar to name key to the groupplot environment, which in turn applies it to each of the plots in the group, so it is executed twice in this case. If you just supply colormap to the last \nextgroupplot[<options>], the message will disappear and the colormap will be placed as desired, without using colorbar to name and \ref. If you do want to use colormap to name, Percusse's answer is a good way to go.

\documentclass[border=4mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[%
point meta min=0, point meta max=25,
group style={
group size=2 by 1,
yticklabels at=edge left,
ylabels at=edge left
}
]
\nextgroupplot
\nextgroupplot[colorbar]
\end{groupplot}
\end{tikzpicture}
\end{document}

-
Thanks, exactly the type of solution I was looking for! Does this mean that all (or perhaps a large, meaningful subset) of the options that can be applied to the axis environments also can be applied to \nextgroupplot? –  josteinb Apr 17 '12 at 7:44
@JosteinBøFløystad: Yes, all the options that can be supplied to an axis can also be applied to nextgroupplot. The groupplots environment is basically an elaborate wrapper for several full-fledged axis environments. –  Jake Apr 17 '12 at 7:50

This just works and there is manual adjustment involved. Probably, Christian or Jake would give you an automated solution. I put the commonbar into a node and placed it next to the rightmost groupplot.

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[%
point meta min=0, point meta max=25,
group style={group size=2 by 1,
yticklabels at=edge left, ylabels at=edge left,
},
colorbar,
colorbar to name={commonbar},
]
\nextgroupplot

For some reason the labels of the colorbar moves too close to the colormap. I don't know why exactly this is happening.But I'll look into it. I used inner sep=0 and that is inherited by colormap. Removing that option fixes that problem.