3

I would like to add a second y-axis inside a groupplot. If there's a better/easier way to get the plot I want (see figure) I am willing to not use groupplot.

enter image description here

\documentclass[tikz]{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}
\pgfplotsset{every  tick/.style={black,},width=10cm}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
            group style={
        group name=plots,
        group size=1 by 2,
        xlabels at=edge bottom,
        xticklabels at=edge bottom,
        vertical sep=0pt,
        }, xlabel=x,xmin=0, xmax=100,xtick align=outside,xtick pos=left
        ]

\nextgroupplot[ymin=0, ymax=1000, ylabel= y1,height=6cm, ytick align=outside,ytick pos=left]

\nextgroupplot[ymin=0, ymax=10, ylabel= y3, ytick={2,4,6,8},height=4cm,ytick align=outside,ytick pos=left]  

\end{groupplot}
\end{tikzpicture}

\end{document}
5

The pgfplots manual has an example showing how to have two ordinates with a normal axis, and basically the same technique can be used with groupplots.

Some key points:

  • Add two groupplots environments in the same tikzpicture.
  • Set the same xmin and xmax for all axes.
  • Add scale only axis so that scaling options apply just to the axes, and not labels etc.
  • In your code you have ytick pos=left, change this to right for the second groupplots, where the ordinate is on the right side.
  • Use axis y line=left or axis y line=right to print the y axis line only on one side.
  • Similarly, axis x line=none for the second groupplots turns off printing of the x axis line.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}
\pgfplotsset{every  tick/.style={black,},width=10cm}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
            group style={
        group size=1 by 2,
        xlabels at=edge bottom,
        xticklabels at=edge bottom,
        vertical sep=0pt,
        },
        xlabel=$x$,
        xmin=0, xmax=100,
        xtick align=outside,xtick pos=left,
        scale only axis,
        ytick align=outside,
        ytick pos=left,
        axis y line=left
        ]

\nextgroupplot[ymin=0, ymax=1000, ylabel=$y_1$,height=6cm, ytick align=outside,ytick pos=left,axis y line=left]

\nextgroupplot[ymin=0, ymax=10, ylabel=$y_3$, ytick={2,4,6,8},height=4cm,]  

\end{groupplot}

\begin{groupplot}[
        group style={
             group name=plots,
             group size=1 by 2,
             xlabels at=edge bottom,
             xticklabels at=edge bottom,
             vertical sep=0pt,
        },
        xmin=0, xmax=100,
        scale only axis,
        ytick align=outside,
        ytick pos=right,
        axis y line=right,
        axis x line=none
        ]

\nextgroupplot[ymin=0,ymax=500,
               ylabel=$y_2$,
               height=6cm,
]

\nextgroupplot[ymin=0,ymax=100,
               ylabel= $y_4$,
               height=4cm,
               ytick={20,40,60,80}]  

\end{groupplot}
\end{tikzpicture}

\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.