0

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

Patricio
  • 339
  • Thanks for the MWE. Please create separate questions for each of the three sub-question (Thx: I see that you have done that already but this question still has the sub-questions). – Dr. Manuel Kuehner Apr 23 '22 at 17:36
  • Unrelated, the tikz package is not needed in the MWE, you can remove it. – Dr. Manuel Kuehner Apr 23 '22 at 17:37
  • Please also add \usepackage{showframe} to your code and you will see that your diagrams are too wide (not sure if that is the root cause of your problem). I added a screenshot to my answer. – Dr. Manuel Kuehner Apr 23 '22 at 18:19

1 Answers1

3
  • 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

  • I thought there was no need for groupplots for just two plots, because their being on the same paragraph was enough for them to appear side by side. – Patricio Apr 23 '22 at 17:05
  • @Patricio I do not claim that this is the best solution but at least it is a well-documented solution and it took me 5 min without trial and error. – Dr. Manuel Kuehner Apr 23 '22 at 17:35