1

Why is there so much extra space on the vertical axis? This is a constant problem for me. Is there an easy fix? What am I doing wrong?

\documentclass[11 pt]{article}
\pagestyle{empty}
\usepackage{tikz, pgfplots}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=1.5]
\begin{axis}[axis x line=middle, axis y line=middle, samples=200,
axis equal, grid, xmin=0, xmax=9, ymin=0,ymax=4, xtick={0,1,...,9}, ytick={0,1,...,4}
]
\addplot[domain=0:9,thick]{sqrt(x)};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

4 Answers 4

3

Welcome to TeX.SE! Since you used axis equal, I assume you want to have an orthonormal coordinate system. This /pgfplots/axis equal PGF key is equivalent to unit vector ratio=1 1 1, which is the same as unit vector ratio=1 1 (when less than 3 values are provided, trailing ones are implicitly used). The problem for what you want to achieve is that unit vector ratio=1 1 tries to preserve the defined (or default) figure width and height, and in order to do so, since you impose an x/y ratio, it has to increase the limits of one axis (here: the y axis). Fortunately, unit vector ratio*=1 1 is an alternative that prefers adapting the figure size to enlarging limits.

Thus, with unit vector ratio*=1 1 and xmin=0, xmax=9, ymin=0, ymax=4 (values from your example), you'll obtain this:

enter image description here

If you want to fit the curve very tightly, use the same but with ymax=3 instead of ymax=4:

enter image description here

Full code:

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}[scale=1.5]
\begin{axis}[axis x line=middle, axis y line=middle, samples=200,
             unit vector ratio*=1 1, grid, xmin=0, xmax=9, ymin=0, ymax=3,
             xtick={0,...,9}, ytick={0,...,4},
             ]
  \addplot[domain=0:9,thick] {sqrt(x)};
\end{axis}
\end{tikzpicture}

\end{document}

As @Zarko pointed out, several values can be automatically derived from xtick={0,...,9} by pgfplots. Considering that axis lines=middle can replace axis x line=middle, axis y line=middle, the first picture can therefore be obtained with only:

\begin{axis}[axis lines=middle, samples=200, unit vector ratio*=1 1,
             grid, xtick={0,...,9}, ymax=4,
             ]
  \addplot[domain=0:9,thick] {sqrt(x)};
\end{axis}

and the second one, with the same options except ymax=4.

P.S.: your example had many unneeded packages, please remove them next time!

2
  • +1 for unit vector ratio*=1 1. I think that defining xmin, xmax, ymin, ymax` and ytick in the second example is superfluous.
    – Zarko
    Feb 3, 2020 at 16:43
  • Correct and updated, thanks for pointing it out.
    – frougon
    Feb 3, 2020 at 16:51
0

enter image description here Is this okay --simply removed option=axis equal -- rest same

\begin{tikzpicture}[scale=1.5]
\begin{axis}[axis x line=middle, axis y line=middle, samples=200,
 grid, xmin=0, xmax=9, ymin=0,ymax=4, xtick={0,1,...,9}, ytick={0,1,...,4}
]
\addplot[domain=0:9,thick]{sqrt(x)};
\end{axis}
\end{tikzpicture}
1
  • Thanks, but I want to keep the aspect ratio.
    – yoyo
    Feb 3, 2020 at 16:40
0

A small variation of nice @frougon answer:

\documentclass[11 pt]{article}
\pagestyle{empty}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
    \begin{center}
\begin{tikzpicture}[scale=1.5]
\begin{axis}[
    axis lines=middle,
    unit vector ratio*=1 1,
    grid,
    xtick={0,1,...,9},
    xmax=9, 
    ymax=3,
    samples at={0,0.05,...,1,1.5,2,...,9}
]
\addplot[thick]{sqrt(x)};
\end{axis}
\end{tikzpicture}
    \end{center}
\end{document}

As you can see, main differences is use of the samples at={...} instead of defining domain by domain=.... This gives with far less (nonlinear distributed) samples almost the same result:

enter image description here

6
  • Well, if you were using the parabola bend, you'd need only three samples. The problem with both suggestions is that you need to know the graph before you plot it. If you do, then there is the question why you plot it at all.
    – user194703
    Feb 3, 2020 at 18:09
  • @Schrödinger'scat, thank you for comment! Please, extend it to an answer!
    – Zarko
    Feb 3, 2020 at 18:43
  • Why? The question has an excellent answer, which answers what was asked in the question perfectly IMHO. The question is, according to how I read it, not how to draw a parabola/square root with the minimum number of samples. I feel we should not distract from the main question. Any other user going through this thread may get confused, in particular if they are newcomers.
    – user194703
    Feb 3, 2020 at 18:47
  • Well, why then you write the comment?
    – Zarko
    Feb 3, 2020 at 18:48
  • Because you seem to be concerned about reducing the samples, and I added a remark how one can conceivably reduce them further. But yes, all of this is off-topic, if that's what you mean. ;-)
    – user194703
    Feb 3, 2020 at 18:50
0

In case one want a TikZ solution:

enter image description here

\documentclass[11 pt]{article}
\usepackage{tikz,lipsum}
\begin{document}
\lipsum[1]  
\begin{center}
\begin{tikzpicture}
\draw[gray!50] (0,0) grid (9,4);
\draw[stealth-stealth] (0,4)|-(9,0);
\foreach \i in {0,...,9} \path (\i,0) node[below=1mm]{$\i$};
\foreach \j in {1,...,4} \path (0,\j) node[left=1mm]{$\j$};

\draw[thick,smooth,orange,samples=200] plot[domain=0:9] (\x,{sqrt(\x)});
\end{tikzpicture}
\end{center}
\lipsum[1]  
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .