2

I want to draw in TikZ the graph of the next image:

enter image description here

casio
  • 359

2 Answers2

5

Well, considering the amount of extra code besides the plot, I personally think using Tikz would be more appropriate in this case, the code is easier. Although it would be doable with pgfplots as well.

Output

enter image description here

Code

\documentclass[tikz,margin=10pt]{standalone}
\usepackage{amsmath}

\usetikzlibrary{arrows.meta, intersections}

\begin{document}
\begin{tikzpicture}[font=\Large]
\draw[{Stealth}-{Stealth}] (0,-5) -- (0,5) node[above] {$y$};
\draw[{Stealth}-{Stealth}, name path=xaxis] (-2.5,0) -- (10,0) node[right] {$x$};

\foreach \y [evaluate=\y as \ylab using int(\y/2)] in {-4,-2,2,4}{%
    \node[anchor=east] at (0,\y) {\ylab};
    \draw (0,\y) --++ (2mm,0);
}

\foreach \x in {1,...,9}{%
    \draw (\x,0) --++ (0,2mm);
}

\draw[thick, loosely dotted, shorten <= 2.5mm, shorten >= 2mm] (3,0) -- (3,4) node[midway, fill=white] {$A=2$} -- (0,4);
\draw (1,0) -- (1,-5);
\draw (9,0) -- (9,-5);
\draw (1,-4.5) -- (9,-4.5) 
    node[pos=.35, fill=white, font=\Large] {$T=\dfrac{9\pi}{4}-\dfrac{\pi}{4}=2\pi$};

\draw[domain=-0.01:8.01, name path=plot, samples=150, very thick] plot (\x+1,{sin(deg(\x*pi/4))*4});

\node (frc) at (3.5,-2.5) {$\phi=\dfrac{\pi}{4}$};
\draw[-{Stealth}, shorten >=3mm] (frc) -- (1,0);

\foreach \xl/\anchor [count=\xx] in {
    1/north east,
    3/north,
    5/north east,
    7/north,
    9/north west}{%
    \path[name path={line\xl}] (\xl,-5) -- (\xl,5);
    \fill[red,name intersections={of={line\xl} and plot,total=\t}]
    \foreach \s in {1,...,\t}{(intersection-\s) circle (4pt)};
    %
    \ifnum\xl=1
        \node[anchor=\anchor] at (\xl,0) {$\frac{\vphantom{\xl}\pi}{4}$};
    \else
        \node[anchor=\anchor] at (\xl,0) {$\frac{\xl\pi}{4}$};
    \fi
}

\end{tikzpicture}
\end{document}

Animation time!

enter image description here

Alenanno
  • 37,338
  • Also: while I may understand that you have recently started using Tikz, it's always appreciate from answerers that you can at least get the diagram/image started by drawing the easiest parts of it. For example, while the plot is not easy to figure out (even for more advanced users), the rest of this diagram could have been achieved consulting the manual: axis lines, ticks, etc. – Alenanno Aug 13 '16 at 15:06
  • thanks... one question more, how i do the animation time? – casio Aug 14 '16 at 16:57
  • @casio enclose the whole tikzpicture in a foreach and then use the variable from the foreach in the plot. That should create one "page" per frame in the output. – Alenanno Aug 14 '16 at 17:07
  • I'm starting to use TikZ , I think you could give an example of how it's done , so I analyze and learn – casio Aug 14 '16 at 18:03
  • 1
    @casio It's really simple: 1) you should install imageMagick, 2) enclose the whole tikzpicture with for example: \foreach \mshift in {1,1,1,1,1.5,...,8.5}{% <tikzpicture here> }, 3) modify the plot like this: (\x+\mshift,{sin(deg(\x*pi/4))*4}), 4) run this from your Terminal/Prompt (you should move to the directory where the PDF is located): convert -delay 10 -loop 0 -density 300 -scale 400 -alpha remove <file>.pdf <file>.gif – Alenanno Aug 14 '16 at 18:20
  • Very useful code! I like the \foreach \xl/\anchor approach. – Dr. Manuel Kuehner Jun 27 '17 at 20:29
1

Another one with the pgfplots machinery (missing the arrow and the period),

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ylabel=$y$,xlabel=$x$,
axis lines=middle,
xmin=0,xmax = 8,ymin=-3,ymax=3,
minor x tick num = 1,
xtick={0.785,2.356,...,7.069},%some multiples of pi with the last one nudged a bit to the left
domain=pi/4:9*pi/4,
xticklabel={$\frac{\ifnum\ticknum>0\relax\number\numexpr2*\ticknum+1\relax\fi\pi}{4}$},
ytick={-2,-1,1,2}
]
\addplot[mark=*,mark options={red},mark repeat=25,samples=101] {2*sin(deg(x)-45)};
\draw[dotted] ({3*pi/4},0) |- (0,2);
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807