我有一个相当大的图形,有两行图,第一行有一个 y 轴,第二行有另一个 y 轴。我想为这两组 y 轴设置轴限值,而不必\nextgroupplot[ymin=###,ymax=###]
为每个图都写一个,例如使用,\pgfplotsset{group/every plot/.style={ymin=###,ymax=###}}
但这似乎不起作用。
这是我想要实现的一个例子
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=3 by 2,every plot/.style={xmin=0,xmax=10}},width=4cm,height=6cm]
\nextgroupplot[ylabel={Quantity1 [unit1]},ymin=0,ymax=5,title={First conditions}]
\addplot {x};
\nextgroupplot[ymin=0,ymax=5,title={Second conditions}]
\addplot {x};
\nextgroupplot[ymin=0,ymax=5,title={Third conditions}]
\addplot {x};
\nextgroupplot[ylabel={Quantity2 [unit2]},ymin=100,ymax=400]
\addplot {400/x^.5};
\nextgroupplot[ymin=100,ymax=400]
\addplot {400/x^.5};
\nextgroupplot[ymin=100,ymax=400]
\addplot {400/x^.5};
\end{groupplot}
\end{tikzpicture}
\end{document}
这使:
以下是尝试简化的代码(并更正了 LaRiFaRi 的评论):
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=3 by 2,every plot/.style={xmin=0,xmax=10}},width=4cm,height=6cm]
\pgfplotsset{group/every plot/.append style={ymin=0,ymax=5}}
\nextgroupplot[ylabel={Quantity1 [unit1]},title={First conditions}]
\addplot {x};
\nextgroupplot[title={Second conditions}]
\addplot {x};
\nextgroupplot[title={Third conditions}]
\addplot {x};
\pgfplotsset{group/every plot/.style={ymin=100,ymax=400}}
\nextgroupplot[ylabel={Quantity2 [unit2]}]
\addplot {400/x^.5};
\nextgroupplot
\addplot {400/x^.5};
\nextgroupplot
\addplot {400/x^.5};
\end{groupplot}
\end{tikzpicture}
\end{document}
但不幸的是,这会弄乱第二组图的轴。还有其他方法可以设置多个图的轴限值吗?
答案1
我想说这不是一个错误,而是一个功能。使用\pgfplotsset
后第一个\nextgroupplot
(很可能)与使用那个相同里面环境的范围
axis
,因此仅限于此。我说过,我还建议开始一个新groupplot
环境。关于“重复”选项:只需创建一个收集所有共同点的样式,并将差异应用于每个新groupplot
环境。
有关更多详细信息,请查看代码中的注释。
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{
compat=1.3,
% create a style that collects stuff that is all `\nextgroupplot`s in common
my groupplot style/.style={
width=4cm,
height=6cm,
% because of a bug
% <https://sourceforge.net/p/pgfplots/bugs/137/>
% one currently cannot use `group style' in other styles
group/group size=3 by 1,
group/every plot/.style={xmin=0,xmax=10},
},
}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
% apply created style here
my groupplot style,
% state a name for this `groupplot' to have a reference point to
% put the second `groupplot` relative to this one
group/group name=first group,
ymin=0,
ymax=5,
]
\nextgroupplot[ylabel={Quantity1 [unit1]},title={First conditions}]
\addplot {x};
\nextgroupplot[title={Second conditions}]
\addplot {x};
\nextgroupplot[title={Third conditions}]
\addplot {x};
\end{groupplot}
\begin{groupplot}[
my groupplot style,
ymin=100,
ymax=400
]
\nextgroupplot[
ylabel={Quantity2 [unit2]},
% state where the first plot should be plotted (and anchored)
% (Please note that this doesn't work when these options are provided
% to the `groupplot' options)
at={($ (first group c1r1.south west) - (0,10mm) $)},
anchor=north west,
]
\addplot {400/x^.5};
\nextgroupplot
\addplot {400/x^.5};
\nextgroupplot
\addplot {400/x^.5};
\end{groupplot}
\end{tikzpicture}
\end{document}