4

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}

Output

plot picture

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)

Thanks in advance for your help, which will be greatly appreciated!

  • For 2., what do you mean by "better"? – Torbjørn T. Oct 28 '17 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) – bertalanp99 Oct 28 '17 at 20:58
  • 1
    Right, I see. I think that's a perfectly fine way of doing it. (pos=1 is unnecessary by the way.) – Torbjørn T. Oct 28 '17 at 21:00
  • 1
    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. – Torbjørn T. Oct 28 '17 at 21:03
  • 1
    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. – Torbjørn T. Oct 28 '17 at 21:12
3
  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.

output of code

\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}
3

like this?

enter image description here

\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$};
     \addplot + [name path=A] {x^3};
     \addplot + [name path=B] {x^5};
     %
     \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}

enter image description here

  • All right, but how do I put function labels this way? (I am very new to pgfplots, sorry) – bertalanp99 Oct 28 '17 at 20:55
  • 1
    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. – Torbjørn T. Oct 28 '17 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 :) – bertalanp99 Oct 28 '17 at 21:22
  • 1
    @bertalanp99, i edit my answer, i suggest to use a legend. simpler and elegant ... :-). – Zarko Oct 28 '17 at 21:30
  • @Zarko I agree, but what use would be a legend in a B&W document? :/ – bertalanp99 Oct 28 '17 at 23:17

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.