Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
\addplot[scatter, scatter src=y] {x^2};
\nextgroupplot
\addplot[scatter, scatter src=y] {x^2};
\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.

share|improve this question

3 Answers 3

up vote 10 down vote accepted

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
\addplot[scatter, scatter src=y] {x^2};
\nextgroupplot[colorbar]
\addplot[scatter, scatter src=y] {x^2};
\end{groupplot}
\end{tikzpicture}
\end{document}
share|improve this answer
    
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
1  
@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
\addplot[scatter, scatter src=y] {x^2};
\nextgroupplot
\addplot[scatter, scatter src=y] {x^2};
\end{groupplot}
\node[anchor=west,inner sep=0] at([shift={(5mm,0cm)}]group c2r1.east) {\ref{commonbar}};
\end{tikzpicture}
\end{document}

enter image description here

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.

share|improve this answer

Percusse's answer helped me a lot, but the colorbar was not placed perfectly aligned to my plot. I made a slight modification, which works without using an extra \node and without static widths. This is done by placing the colorbar with colorbar style. The colorbar is finally displayed by \pgfplotscolorbarfromname{commonbar}.

\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},
colorbar style={
    at={(group c2r1.north east)},
    anchor=north west
}
]
\nextgroupplot
\addplot[scatter, scatter src=y] {x^2};
\nextgroupplot
\addplot[scatter, scatter src=y] {x^2};
\end{groupplot}
\pgfplotscolorbarfromname{commonbar}
\end{tikzpicture}
\end{document}

Note: I would have made a comment, but I have not enough reputation to do so.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.