pgfplots groupplots 中有两个 x 轴,一个在另一根下方,有两列

pgfplots groupplots 中有两个 x 轴,一个在另一根下方,有两列

我正在按照解释 pgfplots groupplots 中的两个 x 轴,一个在另一个之下但是当情节有两列时,我无法做出同样的事情。

我需要将上部 x 轴上的 0 Hz 刻度与下部 x 轴上的 0 rad/s 刻度相匹配,并且将上部 x 轴上的 1 Hz 刻度与下部 x 轴上的 2*pi rad/s 刻度相匹配。

我的代码如下。我在每个图中只留下了两个点。它们看起来是空的。

有什么建议吗?

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots,units}
\pgfplotsset{compat=1.14}

\begin{filecontents*}{ArbitraryPhaseUncontrolledMag_s21_prototype.csv}
freq,s21_dB
0.01,-0.11462467235855
1,-64.67870768049613
\end{filecontents*}

\begin{filecontents*}{ArbitraryPhaseUncontrolledMag_s11_prototype.csv}
freq,s11_dB
0.01,-15.84224832175662
1,-1.478814217873018e-06
\end{filecontents*}

\begin{filecontents*}{ArbitraryPhaseUncontrolledMag_gd_prototype.csv}
freq,group_delay
0.01495,2.587062551091017
1,0.05680967942229484
\end{filecontents*}

\begin{document}
    \begin{tikzpicture}
        \begin{groupplot}[
            group style={
                group size=2 by 2,
                horizontal sep=2cm,
                vertical sep=0.7cm,
                xlabels at=edge bottom,
                ylabels at=edge left,
            },
%        scale only axis,
%        scaled x ticks=false,
%        change x base,
%        x unit=Hz,
%        xlabel={Frequency},
        grid=major,
        ]
        \nextgroupplot[
            ylabel=$ S_{21} $,
            x unit=Hz,
            xlabel={Frequency},
        ]
        \addplot[blue] table[x=freq,y=s21_dB,col sep=comma] {ArbitraryPhaseUncontrolledMag_s21_prototype.csv};
        \addplot[red] table[x=freq,y=s11_dB,col sep=comma] {ArbitraryPhaseUncontrolledMag_s11_prototype.csv};
        \nextgroupplot[
            ylabel=$ \tau_{g} $,
            x unit=Hz,
            xlabel={Frequency},
        ]
        \addplot[blue] table[x=freq,y=group_delay,col sep=comma] {ArbitraryPhaseUncontrolledMag_gd_prototype.csv};
        \nextgroupplot[
            axis y line=none,
            axis x line*=bottom,
            grid=none,
            xlabel=Frequency,
            x unit=rad/s,
            xmin=0,
            xmax=2*pi,
            xtick={0,0.2*2*pi,0.4*2*pi,0.6*2*pi,0.8*2*pi,2*pi},
            height=2.3cm
        ]
        \nextgroupplot[
            axis y line=none,
            axis x line*=bottom,
            grid=none,
            xlabel=Frequency,
            x unit=rad/s,
            xmin=0,
            xmax=2*pi,
            xtick={0,0.2*2*pi,0.4*2*pi,0.6*2*pi,0.8*2*pi,2*pi},
            height=2.3cm
        ]
    \end{groupplot}
    \end{tikzpicture}
\end{document}

答案1

如果你width在组图中添加一个选项,那么你将获得一个适当大小的额外轴。在下面的代码中,我使用了

 width=.45\textwidth,

您还需要强制enlargelimits=true与主图中的样式相匹配。请注意width=.4\textwidth,像我最初所做的那样,减少到确实留出了足够的空间来打印所有刻度,因此pgfplots会忽略您在这种大小下的刻度规范。

示例输出

\documentclass{article}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots,units}
\pgfplotsset{compat=1.14}

\begin{filecontents*}{ArbitraryPhaseUncontrolledMag_s21_prototype.csv}
freq,s21_dB
0.01,-0.11462467235855
1,-64.67870768049613
\end{filecontents*}

\begin{filecontents*}{ArbitraryPhaseUncontrolledMag_s11_prototype.csv}
freq,s11_dB
0.01,-15.84224832175662
1,-1.478814217873018e-06
\end{filecontents*}

\begin{filecontents*}{ArbitraryPhaseUncontrolledMag_gd_prototype.csv}
freq,group_delay
0.01495,2.587062551091017
1,0.05680967942229484
\end{filecontents*}

\begin{document}
    \begin{tikzpicture}
        \begin{groupplot}[
            group style={
                group size=2 by 2,
                horizontal sep=2cm,
                vertical sep=0.7cm,
                xlabels at=edge bottom,
                ylabels at=edge left,
            },
        width=.4\textwidth,
        scale only axis,
        scaled x ticks=false,
        change x base,
        x unit=Hz,
        xlabel={Frequency},
        grid=major,
        ]
        \nextgroupplot[
            ylabel=$ S_{21} $,
            x unit=Hz,
            xlabel={Frequency},
        ]
        \addplot[blue] table[x=freq,y=s21_dB,col sep=comma] {ArbitraryPhaseUncontrolledMag_s21_prototype.csv};
        \addplot[red] table[x=freq,y=s11_dB,col sep=comma] {ArbitraryPhaseUncontrolledMag_s11_prototype.csv};
        \nextgroupplot[
            ylabel=$ \tau_{g} $,
            x unit=Hz,
            xlabel={Frequency},
        ]
        \addplot[blue] table[x=freq,y=group_delay,col sep=comma] {ArbitraryPhaseUncontrolledMag_gd_prototype.csv};
        \nextgroupplot[
            axis y line=none,
            axis x line*=bottom,
            grid=none,
            xlabel=Frequency,
            x unit=rad/s,
            xmin=0,
            xmax=2*pi,
            xtick={0,0.2*2*pi,0.4*2*pi,0.6*2*pi,0.8*2*pi,2*pi},
            height=2.3cm,
            enlargelimits=true
        ]
        \nextgroupplot[
            axis y line=none,
            axis x line*=bottom,
            grid=none,
            xlabel=Frequency,
            x unit=rad/s,
            xmin=0,
            xmax=2*pi,
            xtick={0,0.2*2*pi,0.4*2*pi,0.6*2*pi,0.8*2*pi,2*pi},
            height=2.3cm,
            enlargelimits=true
        ]
    \end{groupplot}
    \end{tikzpicture}
\end{document}

\pgfplotsset可以组合几个选项,特别是对于新注释中的标签样式,并且可以通过直接使用和引入自己的样式命令来缩短代码,如下所示:

第二个示例

\documentclass{article}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots,units}
\pgfplotsset{compat=1.15}

\begin{filecontents*}{ArbitraryPhaseUncontrolledMag_s21_prototype.csv}
freq,s21_dB
0.01,-0.11462467235855
1,-64.67870768049613
\end{filecontents*}

\begin{filecontents*}{ArbitraryPhaseUncontrolledMag_s11_prototype.csv}
freq,s11_dB
0.01,-15.84224832175662
1,-1.478814217873018e-06
\end{filecontents*}

\begin{filecontents*}{ArbitraryPhaseUncontrolledMag_gd_prototype.csv}
freq,group_delay
0.01495,2.587062551091017
1,0.05680967942229484
\end{filecontents*}

\begin{document}
  \begin{tikzpicture}
    \pgfplotsset{mysqstyle/.style={x unit=Hz,
        xlabel={Frequency}},
      myxstyle/.style={axis y line=none,
       axis x line*=bottom,
       grid=none,
       xlabel=Frequency,
       x unit=rad/s,
       xmin=0,xmax=10,
       ymin=0,ymax=0,
       xtick distance=2,
       xticklabel={\ifdim\tick pt=0pt $0$
        \else\ifdim\tick pt=10pt $2\pi$
        \else$\frac{\pgfmathprintnumber{\tick}\pi}{5}$\fi\fi},
       height=1.3cm},
      table/x={freq},
      table/col sep=comma}
    \begin{groupplot}[
      group style={
        group size=2 by 2,
        horizontal sep=2cm,
        vertical sep=0.7cm,
        xlabels at=edge bottom,
        ylabels at=edge left},
      width=.4\textwidth,
      scale only axis,
      scaled x ticks=false,
      change x base,
      grid=major,
      enlargelimits=true]
      \nextgroupplot[ylabel=$S_{21}$,mysqstyle]
        \addplot[blue] table[y=s21_dB]
          {ArbitraryPhaseUncontrolledMag_s21_prototype.csv};
        \addplot[red] table[y=s11_dB]
          {ArbitraryPhaseUncontrolledMag_s11_prototype.csv};
      \nextgroupplot[ylabel=$\tau_{g}$,mysqstyle]
        \addplot[blue] table[y=group_delay]
          {ArbitraryPhaseUncontrolledMag_gd_prototype.csv};
      \nextgroupplot[myxstyle]
      \nextgroupplot[myxstyle]
    \end{groupplot}
    \end{tikzpicture}
\end{document}

相关内容