# Why does TikZ with pgfplots trim my graph like this?

## Code:

\begin{tikzpicture}
\begin{axis}
[
xlabel=$x$, ylabel=$y$,
xmin=-2, xmax=2, ymin=-4, ymax=4,
domain=-2:2,
restrict y to domain=-4:4,
grid=both,
clip=true,
axis lines=middle,
enlarge y limits={rel=0.1},
enlarge x limits={rel=0.1}
]
\addplot [mark=none, thick, smooth] {x}   node [pos=1, above] {$x$};
\addplot [mark=none, thick, smooth] {x^3} node [pos=1, above] {$x^3$};
\addplot [mark=none, thick, smooth] {x^5} node [pos=1, above] {$x^5$};
\end{axis}
\end{tikzpicture}


## Questions

1. Why is the plot of x^5 trimmed so much on the y axis? I'd like all functions to be visible from -4 to +4 on y.

2. Is the way I add labels to my functions fine? Actually, is there a 'preferred way' to do it?

3. Finally, can I somehow set the grid so that like on the x axis, there's a gridline for each integer? (Right now, I only see horizontal lines at -4, -2, +2, +4. Sorry, I don't know how to phrase this better, I am not a native English speaker; hope you understand)

• For 2., what do you mean by "better"? Oct 28, 2017 at 20:53
• @TorbjørnT. Sorry, 'better' really wasn't an accurate description. I was trying to ask if this would be a 'preferred' way to put function labels. I am referring to those, above the curves of my functions. (I have also updated my question, it's now clearer, I hope)
– bp99
Oct 28, 2017 at 20:58
• Right, I see. I think that's a perfectly fine way of doing it. (pos=1 is unnecessary by the way.) Oct 28, 2017 at 21:00
• The pos key is used if you want to place a node at an arbitrary position along the path. pos=0 is at the start of a path, pos=0.5 is halfway, pos=1 is at the end. But when you place a node at the end of a command like in your case, pos=1 is the default, hence not needed. Oct 28, 2017 at 21:03
• Basically, the problem in 1. is that pgfplots uses just 25 samples for a plot by default, in combination with restrict y to domain. With domain=-2:2, the function is calculated at x=-2, x=-1.833, x=1.667, etc. (The step is 4/24=1/6.) So you get a point at x=1.667, giving y=2.16, and the next at x=1.333, giving y=4.21. But because of restrict y to domain=-4:4, all y-values above 4 are replaced by nan, and the plot thus ends at the point (1.6667, 2.16). A higher number of samples will help, see my comment on Zarko's answer. Oct 28, 2017 at 21:12

1. Basically, the problem is that pgfplots uses just 25 samples for a plot by default, in combination with restrict y to domain. With domain=-2:2, the function is calculated at x=-2, x=-1.833, x=1.667, etc. (The step is 4/24=1/6.) So you get a point at x=1.667, giving y=2.16, and the next at x=1.333, giving y=4.21. But because of restrict y to domain=-4:4, all y-values above 4 are replaced by nan, and the plot thus ends at the point (1.6667, 2.16). A higher number of samples can help with that.

2. I think this is a perfectly fine way of doing it.

But consider one thing: If you remove enlarge y limits={rel=0.1}, the nodes with the function labels will be removed from sight, because they are outside the axis boundary, and clip=true (which is the default behaviour). So if you don't want to enlarge the limits, you'll need clip=false.

3. If you just want an extra grid line, you can add a minor tick, with minor tick num=1 as in Zarko's answer. If you want a number at each integer, you can set ytick distance=1.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel=$x$, ylabel=$y$,
xmin=-2, xmax=2, ymin=-4, ymax=4,
domain=-2:2,
restrict y to domain=-4:4,
grid=both,
clip=true,
axis lines=middle,
enlarge y limits={rel=0.1},
enlarge x limits={rel=0.1},
ytick distance=1
]
\addplot [mark=none, thick, samples=2] {x}   node [above] {$x$};
\addplot [mark=none, thick, samples=300] {x^3} node [above] {$x^3$};
\addplot [mark=none, thick, samples=400] {x^5} node [above] {$x^5$};
\end{axis}
\end{tikzpicture}
\end{document}


like this?

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel=$x$, ylabel=$y$,
xmin=-2.5, xmax=2.5, ymin=-5, ymax=5, % changed
domain=-2:2,
%         restrict y to domain=-4:4,
grid=both,
minor tick num=1,
clip=true,
axis lines=middle,
%         enlarge y limits={0.2},
%         enlarge x limits={0.2}
legend pos=south east,
]
\addplot +[mark=none, thick, smooth] {x}   ;
\addplot +[mark=none, thick, smooth] {x^3} ;
\addplot +[mark=none, thick, smooth] {x^5} ;
\legend{$x$, $x^3$, $x^5$}
\end{axis}
\end{tikzpicture}
\end{document}

1. it is restrict due to restrict y to domain=-4:4,
2. labels: do you mind tick labels?
3. you like to have tick label at each unit, i.e.: for y axis at -4,-3, ..., 4?

edit: add labels to curves is not so simply, simpler is add legend as is now corrected an above mwe.

addadendum: if use of legend is not satisfactory solution, than as complement to solution of Torbjørn T., where is not need to determine end of curves for their labels position ... with help of intersection library it can be done as follows:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel=$x$, ylabel=$y$,
xmin=-2.5, xmax=2.5, ymin=-5, ymax=5, % changed
domain=-2:2,
grid=both,
minor tick num=1,
axis lines=middle,
smooth,
no marks,
every axis plot post/.append style={thick}
]
\addplot   {x}  node[text=black, right] {$x$};
%
\path[name path=C] (0,5) -- (2,5);
\path[name intersections={of=A and C, by={x3}}]
node[below right] at (x3) {$x^3$};
\path[name intersections={of=B and C, by={x5}}]
node[below  left] at (x5) {$x^5$};
\end{axis}
\end{tikzpicture}
\end{document}


• All right, but how do I put function labels this way? (I am very new to pgfplots, sorry)
– bp99
Oct 28, 2017 at 20:55
• One possibility: \addplot [mark=none, thick, samples=2] {x} node [above] {$x$}; \addplot [mark=none, thick, samples=300] {x^3} node [above] {$x^3$}; \addplot [mark=none, thick, samples=400] {x^5} node [above] {$x^5$}; Edit; and keep the restrict y to domain and enlarge y limits setting. Oct 28, 2017 at 21:12
• @TorbjørnT. Gee, thanks, that solved it! Please consider adding your solution as an answer to my question so that I can select it as the best solution :)
– bp99
Oct 28, 2017 at 21:22
• @bertalanp99, i edit my answer, i suggest to use a legend. simpler and elegant ... :-). Oct 28, 2017 at 21:30
• @Zarko I agree, but what use would be a legend in a B&W document? :/
– bp99
Oct 28, 2017 at 23:17