pgfplots – How to Place Two Graphs Side by Side When One Has a Discontinuity in the Y-Axis

pgfplots

I'm trying to create a two graph Figure with pgfplots. However, the result is not exactly what I want, because I expected the two graphs to appear side by side, but they are oddly aligned instead. In the pgfplot manual (p. 21) one can read

enter image description here
and, therefore, I don't know what I'm doing wrong.

enter image description here

My code is

\documentclass{article}
\usepackage{pgfplots}

\usepgfplotslibrary{fillbetween}

\pgfplotsset{compat = newest}


\begin{document}

\begin{figure}
   \begin{tikzpicture}
      \begin{axis}[
         ticks=none, 
         axis x line=bottom,
         axis y line=left,
         xmin=0,xmax=7,
         ymin=0,ymax=12]
      \addplot[
         domain = 0:7,
         samples =200,
         ] {x};
      \end{axis}
   \end{tikzpicture}
   \begin{tikzpicture}
      \begin{axis}[
         ticks=none, 
         axis x line=bottom,
         axis y line=left,
         axis y discontinuity=crunch,
         xmin=0,xmax=1.3,
         ymin=0.5,ymax=1.3]
      \addplot[
         domain = 0:sqrt(1/3),
         samples =200,
         ] {sqrt(7/6-x^2)};
      \addplot[
         domain = sqrt(1/3):sqrt(7/6),
         fill = gray,
         fill opacity = 0.1,
         samples =200,
         y filter/.expression={x==sqrt(7/6 )?0:y},
         ] {sqrt(7/6-x^2)}\closedcycle;
      \addplot[
         domain = 0:7,
         samples =200,
         ] {sqrt(5/6)};
      \fill[gray, opacity = 0.1] (0,0) -- (0,0.9129) -- (0.5774,0.9129) -- (0.5774,0);
      \end{axis}
   \end{tikzpicture}
\end{figure}

\end{document}

Edit: Is it, perhaps, a consequence of the crunch of the axis in the rightmost graph? I think it is: removing axis y discontinuity=crunch results in the two graphs being aligned.

enter image description here

Adding \usepackage{showframe} shows that the resulting Figure is wider than the margins even when the two diagrams are correctly aligned. However, the misalignement persists when the crunch is present even if the size of the individual plots is reduced to have them fit in the page width.

enter image description here

Best Answer

  • Community Comment: Normally, it is custom on this site to ask "one question per question" :).
  • Avtual Answer: Below, you find a proposal for your first question (side-by-side), using \usepgfplotslibrary{groupplots}.
  • PGFPLOTS Manual Reference: I also added a screenshot from the related section in the manual.

enter image description here

\documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat = 1.18}

\begin{document}

\begin{tikzpicture}
\begin{groupplot}[
    % Shared options for all diagrams.
    group style = {
            group size = 2 by 1
        },
        height = 40mm,
        width = 30mm,
        ]
\nextgroupplot[
         ticks=none, 
         axis x line=bottom,
         axis y line=left,
         xmin = 0,
         xmax = 7,
         ymin = 0,
         ymax = 12,
         ]
\addplot[
         domain = 0:7,
         samples =200,
         ] {x};
\nextgroupplot[
         ticks = none, 
         axis x line = bottom,
         axis y line = left,
         axis y discontinuity = crunch,
         xmin = 0,
         xmax = 1.3,
         ymin = 0.5,
         ymax = 1.3,
         ]
\addplot[
         domain = 0:sqrt(1/3),
         samples = 200,
         ] {sqrt(7/6-x^2)};
\end{groupplot}
\end{tikzpicture}

\end{document}

enter image description here


Remark: \usepackage{showframe} shows that your diagrams are too wide.

enter image description here