创建与多个图重叠的 x 和 y 标签

创建与多个图重叠的 x 和 y 标签

我有一个 5x3 的群图。现在我希望能够“重叠”,也就是说,ylabel所有图都重叠,反之亦然xlabel。这使我能够使群图更小。

目前我有以下代码:http://pastebin.com/0s6CB9HT。这给了我下面的输出。因此,我想要的是能够将标签“相位 [*] 幅度 [dB]”拉伸到所有图上,作为“一般”标签ylabel。并将“频率 [Hz]”拉伸到xlabel

我怎样才能实现这个目标?

目前我只是将设置ylabel{Phase [*] Magnitude [dB]\\Out2}xlabel设置为{Frequency [Hz]}。并对进行了一些xshift操作ylabel,以使“Out i”彼此对齐。对于 5x3 来说,这现在看起来不错,但如果我有一个 2x2 组图,那么中间就没有图了。

在此处输入图片描述

答案1

简短回答:将以下代码片段粘贴到您的序言中,然后使用定义您的整体标签groupplot ylabel=<label>(可以使用设置样式every groupplot y label/.style):

\makeatletter
\pgfplotsset{
    groupplot xlabel/.initial={},
    every groupplot x label/.style={
        at={($({\pgfplots@group@name\space c1r\pgfplots@[email protected]}|-{\pgfplots@group@name\space c1r\pgfplots@[email protected] south})!0.5!({\pgfplots@group@name\space c\pgfplots@group@columns r\pgfplots@[email protected]}|-{\pgfplots@group@name\space c\pgfplots@group@columns r\pgfplots@[email protected] south})$)},
        anchor=north,
    },
    groupplot ylabel/.initial={},
    every groupplot y label/.style={
            rotate=90,
        at={($({\pgfplots@group@name\space c1r1.north}-|{\pgfplots@group@name\space c1r1.outer
west})!0.5!({\pgfplots@group@name\space c1r\pgfplots@[email protected]}-|{\pgfplots@group@name\space c1r\pgfplots@[email protected] west})$)},
        anchor=south
    },
    execute at end groupplot/.code={%
      \node [/pgfplots/every groupplot x label]
{\pgfkeysvalueof{/pgfplots/groupplot xlabel}};  
      \node [/pgfplots/every groupplot y label] 
{\pgfkeysvalueof{/pgfplots/groupplot ylabel}};  
    }
}

\def\endpgfplots@environment@groupplot{%
    \endpgfplots@environment@opt%
    \pgfkeys{/pgfplots/execute at end groupplot}%
    \endgroup%
}
\makeatother


解释:

要“正确”地定位标签,您可以使用环境groupplotcalc表达式中的每个轴定义的节点和锚点:该点($(group c1r1.north west)!0.5!(group c1r6.south west)$)位于图的左边缘,恰好位于左上图的左上角和左下图的左下角之间的中间位置(如果您有六行图)。要使此解决方案自动化,而无需对行数进行硬编码,您可以使用宏\pgfplots@group@rows,该宏存储环境中的总行数groupplot。但是,这有一个小问题:只有在环境完成后,锚点才会被正确定义groupplot。那时,\pgfplots@group@rows宏不再保存行数。为了解决这个问题,我们可以\endpgfplots@environment@groupplot稍微重新定义宏以包含一个钩子来执行execute at end groupplot键中指定的代码。


完整代码(注意:添加条件,以便刻度标签仅出现在外部图上 - HVennekate)

\documentclass[border=5mm]{standalone}
\usepackage{graphics}
\usepackage{siunitx}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}


\makeatletter
\pgfplotsset{
    groupplot xlabel/.initial={},
    every groupplot x label/.style={
        at={($({group c1r\pgfplots@[email protected]}|-{group c1r\pgfplots@[email protected] south})!0.5!({group c\pgfplots@group@columns r\pgfplots@[email protected]}|-{group c\pgfplots@group@columns r\pgfplots@[email protected] south})$)},
        anchor=north,
    },
    groupplot ylabel/.initial={},
    every groupplot y label/.style={
            rotate=90,
        at={($({group c1r1.north}-|{group c1r1.outer
west})!0.5!({group c1r\pgfplots@[email protected]}-|{group c1r\pgfplots@[email protected] west})$)},
        anchor=south
    },
    execute at end groupplot/.code={%
      \node [/pgfplots/every groupplot x label]
{\pgfkeysvalueof{/pgfplots/groupplot xlabel}};  
      \node [/pgfplots/every groupplot y label] 
{\pgfkeysvalueof{/pgfplots/groupplot ylabel}};  
    },
    group/only outer labels/.style =
{
group/every plot/.code = {%
    \ifnum\pgfplots@group@current@row=\pgfplots@group@rows\else%
        \pgfkeys{xticklabels = {}, xlabel = {}}\fi%
    \ifnum\pgfplots@group@current@column=1\else%
        \pgfkeys{yticklabels = {}, ylabel = {}}\fi%
}
}
}

\def\endpgfplots@environment@groupplot{%
    \endpgfplots@environment@opt%
    \pgfkeys{/pgfplots/execute at end groupplot}%
    \endgroup%
}
\makeatother


\begin{document}
\begin{tikzpicture}

\pgfplotsset{%
  width=4cm,
  height=2cm,
  scale only axis,
  xmajorgrids,
  xminorgrids,
  ymajorgrids,
  yminorgrids
}

\begin{groupplot}[%
  group style={group size=3 by 6,
  horizontal sep=5pt,
  vertical sep=5pt},
  xmode=log,
  groupplot ylabel={Phase [\si{\degree}]\quad Magnitude [\si{\decibel}]},
  groupplot xlabel={Frequency [\si{\hertz}]},
  group/only outer labels
]

% ROW 1
\nextgroupplot[
  ylabel={Out 1},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200,
  title={In 1}
]

\nextgroupplot[
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200,
  title={In 2}
]

\nextgroupplot[
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200,
  title={In 3}
]

% ROW 2
\nextgroupplot[
  ytick={-180,-90,0,90,180},
  ylabel={Out 1},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

\nextgroupplot[
  ytick={-180,-90,0,90,180},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

\nextgroupplot[
  ytick={-180,-90,0,90,180},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

% ROW 3
\nextgroupplot[
  ylabel={Out 2},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

\nextgroupplot[
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

\nextgroupplot[
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

% ROW 4
\nextgroupplot[
  ylabel={Out 2},
  ytick={-180,-90,0,90,180},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

\nextgroupplot[
  ytick={-180,-90,0,90,180},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

\nextgroupplot[
  ytick={-180,-90,0,90,180},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

% ROW 5
\nextgroupplot[
  ylabel={Out 3},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

\nextgroupplot[
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

\nextgroupplot[
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

% ROW 6
\nextgroupplot[
  ylabel={Out 3},
  ytick={-360,-180,0},
  xmin=0.1,
  xmax=100000,
  ymin=-380,
  ymax=20
]

\nextgroupplot[
  ytick={-360,-180,0},
  xmin=0.1,
  xmax=100000,
  ymin=-380,
  ymax=20
]

\nextgroupplot[
  ytick={-360,-180,0},
  xmin=0.1,
  xmax=100000,
  ymin=-380,
  ymax=20
]
\makeatletter
\end{groupplot}



\end{tikzpicture}

\end{document}

答案2

你不需要使用 Tikz 添加标签,然后可以将它们添加到构造的框中。

\documentclass{standalone}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{array}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\begin{document}
\sbox8{\begin{tikzpicture}

\pgfplotsset{%
  width=4cm,
  height=2cm,
  scale only axis,
  every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
  xmajorgrids,
  xminorgrids,
  every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
  ymajorgrids,
  yminorgrids,
  every axis x label/.style={at={(0.5,0)}, align=center, yshift=-28pt},
  every axis y label/.style={at={(0,0.5)}, align=center, xshift=-38pt,
rotate=90}
}

\begin{groupplot}[%
  group style={group size=3 by 6,
  horizontal sep=5pt,
  vertical sep=5pt},
  xmode=log
]

% ROW 1
\nextgroupplot[
  xticklabels={},
% yticklabels={},
  xlabel={},
  ylabel={Out 1},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200,
  title={In 1}
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200,
  title={In 2}
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200,
  title={In 3}
]

% ROW 2
\nextgroupplot[
  xticklabels={},
% yticklabels={},
  ytick={-180,-90,0,90,180},
  xlabel={},
  ylabel={Out 1},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  ytick={-180,-90,0,90,180},
  xlabel={},
  ylabel={},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  ytick={-180,-90,0,90,180},
  xlabel={},
  ylabel={},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

% ROW 3
\nextgroupplot[
  xticklabels={},
% yticklabels={},
  xlabel={},
  ylabel={Out 2},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

% ROW 4
\nextgroupplot[
  xticklabels={},
% yticklabels={},
  xlabel={},
  ylabel={Out 2},
  ytick={-180,-90,0,90,180},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  ytick={-180,-90,0,90,180},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  ytick={-180,-90,0,90,180},
  xmin=0.1,
  xmax=100000,
  ymin=-200,
  ymax=200
]

% ROW 5
\nextgroupplot[
  xticklabels={},
% yticklabels={},
  xlabel={},
  ylabel={Out 3},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

\nextgroupplot[
  xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  xmin=0.1,
  xmax=100000,
  ymin=-400,
  ymax=200
]

% ROW 6
\nextgroupplot[
% xticklabels={},
% yticklabels={},
  xlabel={},
  ylabel={Out 3},
  ytick={-360,-180,0},
  xmin=0.1,
  xmax=100000,
  ymin=-380,
  ymax=20
]

\nextgroupplot[
% xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  ytick={-360,-180,0},
  xmin=0.1,
  xmax=100000,
  ymin=-380,
  ymax=20
]

\nextgroupplot[
% xticklabels={},
  yticklabels={},
  xlabel={},
  ylabel={},
  ytick={-360,-180,0},
  xmin=0.1,
  xmax=100000,
  ymin=-380,
  ymax=20
]

\end{groupplot}

\end{tikzpicture}}

\mbox{%
\rotatebox{90}{\makebox[\ht8]{Phase [\si{\degree}]  Magnitude [\si{\decibel}]}}
\begin{tabular}[t]{c}\usebox8\\Frequency [\si{\hertz}]\end{tabular}}

\end{document}

相关内容