[Tex/LaTex] Common scale for pgfplots groupplot

groupplotspgfplots

The groupplots environment allows global values of xmax to be set. Is there a way to automatically select a common value for all plots in a group? For example, with the code below, the 4th plot does not have the same x-scale as the other 3 plots. Is it possible to have all 4 plots use the same x-axis without manually defining a maximum x value (in this case, '2')?

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 2}]
\nextgroupplot
\addplot coordinates{(0,0)(1,1)(2,2)};
\nextgroupplot
\addplot coordinates{(0,2)(1,1)(2,0)};
\nextgroupplot
\addplot coordinates{(0,2)(1,1)(2,1)};
\nextgroupplot
\addplot coordinates{(0,2)(1,1)(1,0)};
\end{groupplot}
\end{tikzpicture}
\end{document}

Best Answer

You can use the every plot/.style key to enter the common settings in that group

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 2,
                               every plot/.style={
                                        xmin=-1,
                                        xmax=2,
                                        enlargelimits=true
                                 }
                             }]
\nextgroupplot
\addplot coordinates{(0,0)(1,1)(2,2)};
\nextgroupplot
\addplot coordinates{(0,2)(1,1)(2,0)};
\nextgroupplot
\addplot coordinates{(0,2)(1,1)(2,1)};
\nextgroupplot
\addplot coordinates{(0,2)(1,1)(1,0)};
\end{groupplot}
\end{tikzpicture}
\end{document}

enter image description here

Related Question