如何向组图中添加零线?

如何向组图中添加零线?

我想通过定义它们来向组图添加零线一次groupplot环境中。

我当然是根据在这里和手册中找到的线索和答复来做到这一点的,但到目前为止我还无法让它发挥作用。

平均能量损失

\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}

答案1

这是一个可能的解决方案。OP 的方法不起作用的原因在于 groupplots 为每个由 标识的图提供了其内部名称c<column>r<row>。此外,它还使用c<column>r<row>.<anchor position>来标识它们各自的位置。由于每个图的内部名称仅在绘制完图后才为人所知,因此必须在 groupplots 结束后绘制零线。

在此处输入图片描述

代码

\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}

答案2

做了一些测试与评估并找到了解决方案:

简而言之:

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});
},

图片

在此处输入图片描述

平均能量损失

\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}

相关内容