3

I'd like to add zero lines to a groupplot by defining them once in the groupplot environment.

I of course based this on the clues and replies I found on here & in the manual, but so far I couldn't get it to work.

MWE

\documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
amsmath,
tikz,
pgfplots,
pgfplotstable
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{groupplot}[
group style={
group size=1 by 2,
horizontal sep=0cm,
vertical sep=1.5cm
},
%
execute at begin axis/.append code={ %<-this line works for the normal 'axis'
%the following 3 do not produce an error but do not work either:
%execute at every axis/.append code={
%execute at every plot/.append code={
%execute at begin plot/.append code={
\draw[thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
\draw[thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
},
%
height=6cm,
width=14cm,
%
scaled x ticks=true,
scaled y ticks=false,
%
xlabel={The label for the x-axis},
ylabel={Some y-values},
]
\nextgroupplot
\addplot {rand};
%
\nextgroupplot
\addplot {rand};
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}
henry
  • 6,594

2 Answers2

1

This is a possible solution. The reason that the OP's approach did not work is due to the fact that a groupplots has its internal name for each plots identified by c<column>r<row>. Further it uses c<column>r<row>.<anchor position> to identifies their respective position. Since the internal names for each plot are known only after the plots are drawn, the zero lines have to be drawn after end of groupplots.

enter image description here

Code

\documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
amsmath,
tikz,
pgfplots,
pgfplotstable,
}
\usepackage[paper size={20cm,20cm}]{geometry}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{groupplot}[
group style={
group size=1 by 2,
horizontal sep=0cm,
vertical sep=1.5cm,
%xlabels at=edge bottom,
%ylabels at=edge left,
},
height=6cm,
width=14cm,
scaled x ticks=true,
scaled y ticks=false,
%
xlabel={The label for the x-axis},
ylabel={Some y-values},
ymin=-1.5,ymax=1.5,  % a trick to get symmetric y axis so that its center is fixed. 
]
\nextgroupplot
\addplot {rand};
\nextgroupplot
\addplot {rand};
\end{groupplot}
execute at every axis/.append code={ %  what follows works for group plot, but must 
                                     %  be moved to here after the groupplot is done 
                                     %  so that their respective locations are known.
                                     %  the following 3 lines also work:
%execute at every axis/.append code={
%execute at every plot/.append code={
%execute at begin plot/.append code={
\draw [ultra thick, draw=red]  (group c1r1.west) -- (group c1r1.east);
\draw [ultra thick, draw=red]  (group c1r2.west) -- (group c1r2.east);
}
\end{tikzpicture}
\end{center}
\end{document}
Jesse
  • 29,686
  • The execute at every axis-bit isn't necessary at this point. However this code only makes sense with the necessary bit for getting a symmetric share on the y-axis which defeats the purpose of the easily automated solution. Besides, it may end up looking really weird, depending on the data (which is does in my real document). Thank you very much for your effort. – henry Jul 20 '14 at 13:53
  • 1
    Yes, agree with your observations. I, later on, figured that out during my study -- the random plot makes the plot windows dynamic. – Jesse Jul 20 '14 at 14:08
1

Did some more T&E and found the solution:

In short:

execute at begin plot={
\draw[thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
\draw[thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
},

Picture

enter image description here

MWE

\documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
amsmath,
tikz,
pgfplots,
pgfplotstable
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{groupplot}[
group style={
group size=1 by 2,
horizontal sep=0cm,
vertical sep=1.5cm
},
%
execute at begin plot={
%the following ones do not produce an error but do not work either:
%execute at begin group/.append code={
%execute at every axis/.append code={
%execute at every plot/.append code={
%execute at begin plot/.append code={
\draw[thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
\draw[thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
},
%
height=6cm,
width=14cm,
%
scaled x ticks=true,
scaled y ticks=false,
%
xlabel={The label for the x-axis},
ylabel={Some y-values},
]
\nextgroupplot
\addplot {rand};
%
\nextgroupplot
\addplot {rand};
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}
henry
  • 6,594