2

I have the following MWE

\documentclass[tikz,border=2mm]{standalone} 
\begin{document}
\begin{tikzpicture}[domain=-1:6]
\draw[very thin, color=green, step=0.2cm](-2,-3) grid (7,11);
\draw[color=cyan](-2,-3) grid (7,11);
\draw[->,ultra thick] (-2,0)--(7,0) node[below]{$x$};
\draw[->,ultra thick] (0,-3)--(0,11) node[left]{$y$};
\foreach \x in {-1,0,...,6}
\draw (\x cm, 1pt)--(\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {-2,-1,...,10}
\draw (1pt, \y cm)--(-1pt, \y cm) node[anchor=east] {$\y$};
\draw[color=black, thick] plot (\x,\x^2-5*\x+4) node[]{$y=x^2-5x+4$};
\end{tikzpicture}

\end{document}

The problem is that the first coordinate should have been (-1,10) but my code is plotting (-1,8). Other points appear to be okay. The function is y=x²-5x+4

Please, my requests are as follows:

  1. Help me correct this code so that the graph would pass through (-1,10) as it should, instead of (-1,8)
  2. I would be glad if the label (y=x²-5x+4) is sloped and along the curve instead of hanging on top.

Graph of y=x²-5x+4

  • 1
    Try \draw[color=black, thick] plot (\x,\x*\x-5*\x+4) instead \draw[color=black, thick] plot (\x,\x^2-5*\x+4) – minhthien_2016 Oct 24 at 7:52
  • 1
    Or \draw[color=black, thick,smooth,samples=100] plot (\x,\x*\x-5*\x+4) node[]{$y=x^2-5x+4$}; – minhthien_2016 Oct 24 at 8:01
  • Thank you so much. – Chika Odiliobi Oct 24 at 14:10
  • 1
    @ChikaOdiliobi, if you liked answer, you can upvote it or even accept :-) – Zarko Oct 24 at 14:14
4

Here's an alternative solution with pgfplots, I haven't changed your plot command.

Output

graphic plot image

Code

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.17} 

\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
            domain=-1:6,
            yscale=2,
            axis lines=middle,
            xlabel={\large $x$},
            ylabel={\large $y$},
            xtick={-1,...,6},
            ytick={-2,...,10},
            ymin=-3,
            ymax=11,
            xmin=-2,
            xmax=7,
            grid=both,
            grid style={line width=.1pt, green},
            major grid style={line width=.2pt,cyan},
            minor tick num=4,
            tick style={draw=none},
            extra x ticks={-2,6,7},
            extra y ticks={-3,10,11},
            extra x tick labels={},
            extra y tick labels={},
            extra x tick style={grid=both},
            extra y tick style={grid=both},
        ]
        \addplot[thick,smooth, cyan!50!green] (\x,\x^2-5*\x+4) node[]{$y=x^2-5x+4$};
    \end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |
4

Culprits of your problem is \x^2 in your function. Correct is (\x)^2.

Try:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{tikzpicture}[
arr/.style = {very thick, draw=gray, -{Straight Barb[length=1mm]}},
domain=-1:6, samples=70
                        ]
\draw[very thin, color=green, step=0.2cm](-2,-3) grid (7,11);
\draw[color=cyan](-2,-3) grid (7,11);
\draw[arr] (-2,0) -- (7, 0) node[below]{$x$};
\draw[arr] (0,-3) -- (0,11) node[left]{$y$};
% tick labels
\foreach \x in {-1,0,...,6}
\draw (\x, 1pt) -- ++ (0,-1pt) node[below left] {$\x$};
\foreach \y in {-2,-1,...,10}
\draw (1pt, \y) -- ++ (-1pt, 0) node[below right] {$\y$};
% function
\draw[color=black, thick] plot (\x,{(\x)^2 - 5*\x+4}) 
    node[anchor=north east, rotate=80] {$y=x^2-5x+4$};
\end{tikzpicture}

\end{document}

enter image description here

| improve this answer | |
  • \usetikzlibrary{arrows.meta} need for Straight Barb[length=1mm]. – M.Ahmadi Oct 24 at 10:34
  • @M.Ahmadi, you are right! This line was lost during updating of MWE ... :-(. Corrected now. – Zarko Oct 24 at 12:57
  • Thank you very much – Chika Odiliobi Oct 24 at 14:11
  • @Zarko the line node[xshift=1em, anchor=east, rotate=80] {$y=x^2-5x+4$}; could be improved by replacing the options with node[anchor=north east, below, sloped] {$y=x^2-5x+4$};, I think. – Alenanno Oct 24 at 14:33
  • @Alenanno, checked. You are right. Corrected. Thank you very much! – Zarko Oct 24 at 14:39

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.