# How to stack and align multiple plots with same x but different y axes?

I have several series of datapoints from each of several sites. I would like to produce a series of stacked plots, close together as in the attached example, to allow comparison both within and between series. I produced the example using pgfplots 'groupplot' facility, but it doesn't allow me to have different y axes for each variable.

The pgf manual says you can also group multiple plots in a matrix (which would allow different y axes), but in that case, how can I get the plots to be 'stuck together' as in this example?

This is the code for my example:

% Preview source code

%% LyX 2.0.1 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.

\usepackage {tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\makeatother

\usepackage{babel}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group name=my plots,group size=1 by 3,xlabels at=edge bottom,xticklabels at=edge bottom,ylabels at=edge left,yticklabels at=edge left,vertical sep=0pt},footnotesize,width=8cm,height=3cm,xlabel=Site ,,xmin=-0.5,xmax=10.5,ymin=0,ymax=30,ytick={0,10,...,20},xtick={0,1,...,12},xticklabels={A,B,C,D,E,F,G,H,I,J,K},tickpos=left,ytick align=outside,xtick align=outside]
\nextgroupplot
\addplot [ybar,fill=blue]coordinates{(0,0) (1,9.94) (2,0.72)(3,1.17)(4,1.05)(5,0.67)(6,0.24)(7,1.75)(8,0)(9,0)(10,1.21)(11,0)};
\nextgroupplot
\addplot [ybar,fill=red]coordinates{(0,0) (1,14.04) (2,0.72)(3,0)(4,22.81)(5,3.34)(6,2.05)(7,11.23)(8,2.59)(9,0)(10,0.61)(11,0)};
\nextgroupplot
\addplot [ybar,fill=red]coordinates{(0,0) (1,0) (2,0)(3,0)(4,0.53)(5,0.33)(6,0.73)(7,0)(8,0)(9,0)(10,3.9)(11,0.17)};
\end{groupplot}
\end{tikzpicture}

\end{document}


-

You can adjust axis options for individual plots in a groupplot environment by passing the options to \nextgroupplot:

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=my plots,
group size=1 by 3,
xlabels at=edge bottom,
xticklabels at=edge bottom,
vertical sep=0pt
},
ybar,
footnotesize,
width=8cm,
height=3cm,
xlabel=Site,
xmin=-0.5, xmax=10.5,
ymin=0, ymax=29,
xtick={0,1,...,12},
xticklabels={A,B,C,D,E,F,G,H,I,J,K},
tickpos=left,
ytick align=outside,
xtick align=outside
]
\nextgroupplot[ymax=11]
\addplot [fill=blue] coordinates{(0,0) (1,9.94) (2,0.72)(3,1.17)(4,1.05)(5,0.67)(6,0.24)(7,1.75)(8,0)(9,0)(10,1.21)(11,0)};
\nextgroupplot
\addplot [fill=red] coordinates{(0,0) (1,14.04) (2,0.72)(3,0)(4,22.81)(5,3.34)(6,2.05)(7,11.23)(8,2.59)(9,0)(10,0.61)(11,0)};
\nextgroupplot[ymax=5]
\addplot [fill=red] coordinates{(0,0) (1,0) (2,0)(3,0)(4,0.53)(5,0.33)(6,0.73)(7,0)(8,0)(9,0)(10,3.9)(11,0.17)};
\end{groupplot}
\end{tikzpicture}

\end{document}


-
Very elegant. Didn't realise you could do that. You adjust the height of individual plots too with height=XXcm in the \netgroupplot. Cool! –  moabi2000 Oct 14 '11 at 9:06

You could simply use three separate tikzpicture environments and specify the different y-axis values as you wish. To allow for easier comparison between the graphs, I'd recommend adding y=0.05cm to fix the y unit vector length. This results in the three plots above each other but with vertical space between them. To remove this vertical space we need to set the bounding box to be just the axis frames using \pgfresetboundingbox followed by:

\useasboundingbox (current axis.south west) rectangle (current axis.north east);


This reduced the vertical space separating the plots to almost zero, but did not remove it entirely. So to fix this I added a (0,0.3ex) fudge factor as shown in the code below. There probably is a better way to set this, but as it is this produces the desired result:

\documentclass{article}

\usepackage {tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\pgfkeys{/pgfplots/my group style/.style={
group style={group name=my plots,group size=1 by 4,xlabels at=edge bottom,
xticklabels at=edge bottom,ylabels at=edge left,yticklabels at=edge left,
vertical sep=0pt},
footnotesize,width=8cm,height=3cm,xlabel=Site, xmin=-0.5, xmax=10.5, ymin=0, ymax=30,
ytick={0,10,...,30}, xtick={1,2,...,12}, xticklabels={A,B,C,D,E,F,G,H,I,J,K},
tickpos=left, ytick align=outside, xtick align=outside,
y=0.05cm% Fixed y unit vector to allow for comparrison
}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[my group style,xlabel={},xticklabels={},ymax=25]
\addplot [ybar,fill=blue]coordinates{(0,0) (1,9.94) (2,0.72)(3,1.17)(4,1.05)(5,0.67)(6,0.24)(7,1.75)(8,0)(9,0)(10,1.21)(11,0)};
\end{axis}
\pgfresetboundingbox
\useasboundingbox ($(current axis.south west)+(0,0.3ex)$)
rectangle (current axis.north east);
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[my group style,xlabel={},xticklabels={},ymax=35]
\addplot [ybar,fill=red]coordinates{(0,0) (1,14.04) (2,0.72)(3,0)(4,22.81)(5,3.34)(6,2.05)(7,11.23)(8,2.59)(9,0)(10,0.61)(11,0)};
\end{axis}
\pgfresetboundingbox
\useasboundingbox ($(current axis.south west)+(0,0.3ex)$)
rectangle (current axis.north east);
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[my group style,ymax=25]
\addplot [ybar,fill=red]coordinates{(0,0) (1,0) (2,0)(3,0)(4,0.53)(5,0.33)(6,0.73)(7,0)(8,0)(9,0)(10,3.9)(11,0.17)};
\end{axis}
\pgfresetboundingbox
\useasboundingbox ($(current axis.south west)+(0,0.3ex)$)
rectangle (current axis.north east);
\end{tikzpicture}
\end{document}

-
Works great thank you. This is the solution I used this time, and works very well. –  moabi2000 Oct 14 '11 at 9:06