2

m

I am using pgfplots and tikz, and below is the code I have for now.

\begin{tikzpicture}
    \begin{axis}[
            axis x line=middle,
            axis y line=middle,
            axis line style={=>},
            xmin=-1,xmax=5,
            ymin=-1,ymax=5,
            xlabel=$x$,
            ylabel=$y$,
            xtick=\empty,
            ytick=\empty,
            xticklabels=\empty,
            yticklabels=\empty,
        ]
        \addplot[smooth,very thick,black,-]{-(x-(3/2))^2+(25/8)};
        \draw (0,0) coordinate  
    \end{axis}
\end{tikzpicture}

What is the code for labeling:

  1. the origin with the letter O?
  2. the x-intercept (4,0)
  3. the x and y values of the vertex on the x and y axes
  4. dashed lines leading to the vertex
  5. the label y=f(x) on top of the parabola

Bottomline: I am trying to plot a graph that looks exactly like the one in the image attached.

Thank you.

1

TikZ allows you to declare functions and parses expressions in a path, so you might do:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=-(\x-(3/2))^2+(25/8);}]
    \begin{axis}[
            axis x line=middle,
            axis y line=middle,
            axis line style={=>},
            xmin=-1,xmax=5,
            ymin=-1,ymax=5,
            xlabel=$x$,
            ylabel=$y$,
            xtick=\empty,
            ytick=\empty,
            xticklabels=\empty,
            yticklabels=\empty,
        ]
        \addplot[smooth,very thick,black,-]{f(x)};
        \path (0,0) node[below left]{$O$};
        \draw[dashed] (0,{f(3/2)}) node[left]{$\frac{25}{8}$}-- (3/2,{f(3/2)})
        node[above right]{$y=f(x)$} -- (3/2,0) node[below]{$\frac{3}{2}$};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

5
  • Of course \path (0,0) node[below right]{$O$}; looks better. – user194703 Sep 5 '19 at 19:16
  • Thank you for your help.You are amazing. I have another question. When I add \path (4,0) node[below]{4}; to the code (in order to make the x-intercept explicit), the 4 lies much farther to the right than it should. Is there a way to fix this problem besides brute-force changing \path (4,0) node[below]{4}; to \path (3,0) node[below]{4};? – Sam Sep 5 '19 at 22:56
  • @SamChoi Did you add \path (4,0) node[below]{4}; before or after \end{axis}? If you add it after, then the result is off because the coordinates of the ambient tikzpicture do not coincide with the ones of the axis. If you put it inside, the result looks consistent to me. Note that the parabola crossed the horizontal axis at (6 + 5*sqrt(2))/4, not at 4. – user194703 Sep 5 '19 at 23:14
  • @ Schrödinger's cat I added \path (4,0) node[below]{4}; before the \end{axis}. I just found out that the graph itself is a bit misleading. Should have done the calculation for the x-intercept. Thank you. I have a few more questions, though. 1. What is the code \pgfplotsset{compat=1.16} for? How is it related to \pgfplots exactly? 2. In \addplot[smooth,very thick,black,-]{f(x)};, what is the function of the hyphen (-)? 3. Is there a documentation which outlines the commands and their functions for rendering graphs on the coordinate plane such as the one posted in the question? – Sam Sep 6 '19 at 9:24
  • @SamChoi \pgfplotsset{compat=1.16} indicates the version. pgfplots has changed over the time. For instance, in much older versions we would have had to prepend the coordinates in the paths by axis cs: but from version 1.11 on this is no longer necessary. As for the hyphen: here we could drop it. It says something like "no arrows". If one uses -> instead, there will be an arrow. If you have a scope with arrows, - says for this path one should not have an arrow. – user194703 Sep 6 '19 at 11:07
0

As pure TikZ picture:

\documentclass[tikz,border=3mm]{standalone}

\begin{document}
\begin{tikzpicture}[declare function={f(\x)=-(\x-(3/2))^2+(25/8);}]
\draw[->] (-1,0) -- (4,0) node[below left] {$x$};
\draw[->] (0,-1) -- (0,4) node[below left] {$y$};
\draw[very thick] 
    plot[domain=-0.5:3.5, samples=100] (\x,{f(\x)}); 
\node[above right,inner sep=1pt] at (2,{f(2)}) {$y=f(x)$};
\node[below left, inner sep=1pt] at (0,0) {$O$};
\draw[dashed] 
        (0,{f(3/2)}) node[left]  {$\frac{25}{8}$} 
    -|  (3/2,0)      node[below] {$\frac{ 3}{2}$};
\end{tikzpicture}
\end{document}

enter image description here

1
  • Thank you for your help. You help is much appreciated. – Sam Sep 6 '19 at 9:29

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.