tikzpgfplots

Why is the Graph clipped at x = 5?


I have just started to work with the pgfplots package. I want to draw a graph but it is clipped at the right at x = 5. I have set xmax = 6 because I want it to be drawn to x = 6. What am I doing wrong?

This is my code:

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest, width=7cm, height=7cm,
  layers/axis lines on top/.define layer set={
    axis background,
    axis grid,
    axis ticks,
    pre main,
    main,
    axis lines,
    axis descriptions,
    axis foreground,
    axis tick labels,
  }{/pgfplots/layers/standard},
axis on top=false,
every x tick label/.append style={font=\footnotesize, yshift=0.2ex},
every y tick label/.append style={font=\footnotesize, xshift=0.2ex}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ clip=true, set layers=axis lines on top,
xmin=-3, xmax=6,
ymin=-4, ymax=5,
xtick={-3,...,6},
xticklabels={,-2,-1,,1,2,3,4,5,},
ytick={-4,...,5},
yticklabels={,-3,-2,-1,,1,2,3,4,},
minor tick num=1,
axis lines=middle,
xlabel=$x$,
ylabel=$y$,
every axis plot/.append style={ultra thick},
xmajorgrids=true,
xminorgrids=true,
ymajorgrids=true,
yminorgrids=true,
]
\addplot[line width=1.5pt, color=black, samples=100]{-0.5*x^2+2*x+1};
\node[anchor=north west, fill=gray!50] at (rel axis cs:0,1) {\textbf{3}};
\draw(current axis.south west)rectangle(current axis.north east);
\end{axis}
\end{tikzpicture}
\end{document}

Solution

  • You could explicitly set a domain for plotting:

    \documentclass{standalone}
    \usepackage[utf8]{inputenc}
    \usepackage{tikz,pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{compat=newest, width=7cm, height=7cm,
      layers/axis lines on top/.define layer set={
        axis background,
        axis grid,
        axis ticks,
        pre main,
        main,
        axis lines,
        axis descriptions,
        axis foreground,
        axis tick labels,
      }{/pgfplots/layers/standard},
    axis on top=false,
    every x tick label/.append style={font=\footnotesize, yshift=0.2ex},
    every y tick label/.append style={font=\footnotesize, xshift=0.2ex}}
    \begin{document}
    \begin{tikzpicture}
    \begin{axis}[ clip=true, set layers=axis lines on top,
    xmin=-3, xmax=6,
    ymin=-4, ymax=5,
    xtick={-3,...,6},
    xticklabels={,-2,-1,,1,2,3,4,5,},
    ytick={-4,...,5},
    yticklabels={,-3,-2,-1,,1,2,3,4,},
    minor tick num=1,
    axis lines=middle,
    xlabel=$x$,
    ylabel=$y$,
    every axis plot/.append style={ultra thick},
    xmajorgrids=true,
    xminorgrids=true,
    ymajorgrids=true,
    yminorgrids=true,
    ]
    \addplot[line width=1.5pt, color=black, samples=100,domain=-2:6]{-0.5*x^2+2*x+1};
    \node[anchor=north west, fill=gray!50] at (rel axis cs:0,1) {\textbf{3}};
    \draw(current axis.south west)rectangle(current axis.north east);
    \end{axis}
    \end{tikzpicture}
    \end{document}
    

    enter image description here