Graphics, Figures & TablesRecreating a Figure

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
coachbennett1981
Posts: 274
Joined: Fri Feb 05, 2010 10:15 pm

Recreating a Figure

Post by coachbennett1981 »

I am trying to recreate the attached figure using PGF plots. Is there an easier way to do this? Is there a way to draw or create functions similar to this without having to necessarily know the equation of the function? I have tried using the hobby package and estimate points of the curve, but had no luck.

I know I could screen shot the pic, but it does not look as good. I would really like to learn how to do this more efficiently. I am trying to use quadratic equations, but as you can see my figure is incomplete. I hope it is possible. I also would love to know how to draw the f(x) label node with the arrow.

Any help would be appreciated.

Any help would be appreciated.

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{graphics}
\usepackage{graphicx}
\usetikzlibrary{hobby}
\pgfplotsset{compat=newest}
\tikzset{point/.style={circle,draw=black,inner sep=0pt,minimum size=3pt}}
\usetikzlibrary{arrows.meta}
\usepackage[labelformat=empty]{caption}


\begin{document}

\begin{figure}
    \centering
        \begin{tikzpicture}
                \begin{axis}[grid style={gray!50}, thick,
                    xlabel={\(x\)},
                    ylabel={\(y\)}, xmin=-4,xmax=6,ymin=-5,ymax=5,
            every axis plot/.append style={ultra thick},
            axis y line=center,
            axis x line=center,
            axis line style={Triangle-Triangle},
             ticklabel style={font=\small,fill=white},
             yticklabels=\empty,
              ytick=\empty
            ]
            \addplot[thick,samples=1000,domain=-3:-2]{-2*(x+3)^2+2};
            \addplot[thick,samples=1000,domain=0:2]{-(x-1)^2};

            \addplot[thick,samples=1000,domain=-2:-1]{.5(x+1)^2-2};
            \draw[thick, dashed] (-3,0)--(-3,2);

            \addplot[thick,samples=1000,domain=3:5]{-(x-5)^2+2};
            \draw[thick, dashed] (-3,0)--(-3,2);
            
            \draw[thick,dashed] (5,0)--(5,2);
            \draw[thick,dashed] (3,0)--(3,-3);
            \draw[thick,dashed] (-1,0)--(-1,-2);
            
             \end{axis}

        \end{tikzpicture}
    \caption{}
\end{figure}


\end{document}



Attachments
Screen Shot 2022-12-08 at 7.45.28 PM.png
Screen Shot 2022-12-08 at 7.45.28 PM.png (87.4 KiB) Viewed 3272 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Recreating a Figure

Post by Stefan Kottwitz »

Hi,

you could include the original image as a node, draw a grid to see the coordinates, then choose as many coordinate values as possible and draw a smooth plot, then remove the image node:

Code: Select all

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  %\node[opacity=0.5] at (2.8,1.2) {\includegraphics{screenshot}};
  \draw[thin,dotted] (-8,-5) grid (12,3);
  \draw[->] (-8,0) -- (13,0);
  \draw[->] (0,-5) -- (0,7);
  \draw plot [smooth,tension=0.7] coordinates {
    (-6.3,2.5) (-5.4,2) (-4.7,1) (-4,-0.5) (-3,-3) (-2,-3.6)
    (-1,-3) (0,-1.8) (1,-0.5) (2,0) (3,-0.5) (4,-1.8) (5,-3.2)
    (6.1,-4.1) (7,-3.2) (8,-0.6) (9,1) (10,2) (10.5,2.1)
  };
\end{tikzpicture}
\end{document}
graph.png
graph.png (70.64 KiB) Viewed 3238 times
Stefan
LaTeX.org admin
coachbennett1981
Posts: 274
Joined: Fri Feb 05, 2010 10:15 pm

Recreating a Figure

Post by coachbennett1981 »

Thank you! Do you know how I can draw the y=fprime(x) arrow node?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Recreating a Figure

Post by Stefan Kottwitz »

Simply add a node as usual, again looking at the grid helps with guessing coordinates:

Code: Select all

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{decorations.pathmorphing,arrows.meta}
\begin{document}
\begin{tikzpicture}
  %\node[opacity=0.5] at (2.8,1.2) {\includegraphics{screenshot}};
  \node[font=\LARGE] (y) at (-4,3.5) {$y=f'(x)$};
  \draw[decorate,decoration={coil,segment length=0.7cm,aspect=0},
    >={Triangle},->] (y) -- ++(-1.3,-1.3);
  \draw[thin,dotted] (-8,-5) grid (12,3);
  \draw[->] (-8,0) -- (13,0);
  \draw[->] (0,-5) -- (0,7);
  \draw plot [smooth,tension=0.7] coordinates {
    (-6.3,2.5) (-5.4,2) (-4.7,1) (-4,-0.5) (-3,-3) (-2,-3.6)
    (-1,-3) (0,-1.8) (1,-0.5) (2,0) (3,-0.5) (4,-1.8) (5,-3.2)
    (6.1,-4.1) (7,-3.2) (8,-0.6) (9,1) (10,2) (10.5,2.1)
  };
\end{tikzpicture}
\end{document}
Stefan
LaTeX.org admin
Post Reply