# Common colorbar for groupplot

I want to group two surface plots with the groupplots library from pgfplots and place a common colorbar from the top of the first plot to the bottom of plot.

At the moment (code below), both plots have their own colorbar, which is not what I want.

In know, that there is the possibility to put the colorbar to name. But this decouples the plot and the colorbar and is also not what I want. The second thing, I thought of, was to put the colorbar in the group style. But there, pgfplots complains, that it doesn't know the key.

So, how do I create such a colorbar?

The following code shows the problem.

\documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pagestyle{empty}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[view={0}{90},
xlabel=$x$,
ylabel=$y$,
colorbar right, % This creates a colorbar next to each plot.
group/xlabels at = edge bottom,
group style = {group size = 1 by 2,
xlabels at = edge bottom
}]
\nextgroupplot
\nextgroupplot
\end{groupplot}
\end{tikzpicture}
\end{document}


You simply need to adjust the height of the color bar plot.

This can easily be accomplished by doing:

every colorbar/.append style={height=<height specification>}


However, often you will use different heights for different groupplots. Therefore a more general approach would be:

every colorbar/.append style={height=
2*\pgfkeysvalueof{/pgfplots/parent axis height}+
\pgfkeysvalueof{/pgfplots/group/vertical sep}
}


The first key /pgfplots/parent axis height is the height of the actual contour plot (of which you attach the colorbar).

The second key /pgfplots/group/vertical sep is the spacing in between the two group plots. This needs to be adjusted by the number of lower lying plots, in this case only one.

So the above takes and makes the colorbar plot have a height equal to 2 plots plus the vertical separation of the plots. Furthermore it is generic if you change height and vertical sep, etc. However, notice that if the plots have different heights the above approach will not work.

Ok, so one thing is that you only need to plot the colorbar once. Groupplots ensure that each statement in the groupplot environment will be added to every sub-plot. Hence you simply need to put the colorbar in the top \nextgroupplot. The colorbar is aligned at the top right corner of the parent axis, hence you should do it to the top plot.
So:

\nextgroupplot[colorbar right,
every colorbar/.append style={height=
2*\pgfkeysvalueof{/pgfplots/parent axis height}+
\pgfkeysvalueof{/pgfplots/group/vertical sep}}]


A thing to be very careful about when attaching colorbars to several contours is to ensure the bounds of the contour to be the same for both, hence I would recommend you to also add point meta min=<value> and point meta max=<value>. Please do remember this! :)

Ok, so the final groupplots is something like this:

\begin{tikzpicture}
\begin{groupplot}[view={0}{90},
xlabel=$x$,
ylabel=$y$,
height=3cm,
point meta min=-1.5,
point meta max=1.5,
group/xlabels at = edge bottom,
group style = {group size = 1 by 2,
xlabels at = edge bottom
}]
\nextgroupplot[colorbar right,
every colorbar/.append style={height=
2*\pgfkeysvalueof{/pgfplots/parent axis height}+
\pgfkeysvalueof{/pgfplots/group/vertical sep}}]