4

I was following the explained in Two x-axis, one below the other, in a pgfplots groupplots, but I am unable to make the same thing when the plot has two columns.

I need to match the 0 Hz tick in the upper x-axis with the 0 rad/s tick in the lower x-axis, and the same with the 1 Hz tick in the upper x-axis and the 2*pi rad/s tick in the lower x-axis.

My code follows below. I left just two points in each plot. They will seem empty.

Any suggestion?

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

2

If you add a width option to your group plot then you get an extra axis of the appropriate size. In the code below I have used

 width=.45\textwidth,

You also need to enforce enlargelimits=true to match up with the style in the main plots. Note reducing to width=.4\textwidth, as I originally had, does leave enough room for printing all the ticks, so pgfplots ignores your tick specification at such a size.

Sample output

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

Several options can be combined, particularly for the label style in your new comment, and the code shortened by using \pgfplotsset directly and by introducing your own style commands as follows:

Second sample

\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}
Andrew Swann
  • 95,762
  • Thank you! Now the lower axis has the right size. However, why it still doesn't show the correct values in the lower axis? And, if the previous issue could be corrected, is there a way to match the starting and ending ticks of the lower and upper axis, so as the relationship between the two axes be correct? – Joao Alberto Mar 28 '18 at 11:36
  • @JoaoAlberto Yes, I had noticed that too. It is now fixed. – Andrew Swann Mar 28 '18 at 11:56
  • Thanks again, @AndrewSwann! But still lasts the issue with the values of the lower axis. Why doesn't it accept the values of xmax and xtick for the below axis? – Joao Alberto Mar 28 '18 at 12:16
  • I got it! An arbitrary ymin and ymax were missing for the last two graphs (the lower x-axis). I changed the following line xtick={0,0.2*2*pi,0.4*2*pi,0.6*2*pi,0.8*2*pi,2*pi}, by xtick={0,1.256637,2.513274,3.769911,5.026548,6.2831853}, xticklabels={0,$\frac{2\pi}{5}$,$\frac{4\pi}{5}$,$\frac{6\pi}{5}$,$\frac{8\pi}{5}$,$2\pi$}, ymin=0, ymax=0, – Joao Alberto Mar 28 '18 at 13:32