如何在 tikz 上绘制正弦函数的延伸?

如何在 tikz 上绘制正弦函数的延伸?

我想重新创建下图

在此处输入图片描述

这是我的 MWE

\documentclass{article}
\usepackage{subcaption}
\usepackage[labelformat=parens,labelsep=quad,skip=3pt]{caption}
\usepackage{graphicx}
\usepackage{amsmath,tikz}

\usetikzlibrary{arrows.meta, intersections}

\begin{document}

\begin{figure}
  \begin{subfigure}{6cm}
  \begin{tikzpicture}[font=\Large]
\draw[-{Stealth}] (1,-1) -- (1,5) node[above] {$y$};
\draw[-{Stealth}, name path=xaxis] (-1,0) -- (8,0) node[right] {$x$};
\draw[domain=-0.01:3.15, name path=plot, samples=500, very thick] plot (\x+1,{sin(deg(\x))*4});
\end{tikzpicture}
\caption{$f(x)=\sin x, 0\leq x\leq 0$}
  \end{subfigure}
  \begin{subfigure}{6cm}
\begin{tikzpicture}[font=\Large]
\draw[-{Stealth}] (1,-5) -- (1,5) node[above] {$y$};
\draw[-{Stealth}, name path=xaxis] (-6.4,0) -- (8,0) node[right] {$x$};
\draw[domain=-6.285:6.4, name path=plot, samples=500, very thick] plot (\x+1,{sin(deg(\x))*4});
\end{tikzpicture}
\caption{Odd extension of $f(x)=\sin x$}
  \end{subfigure}

  \begin{subfigure}{6cm}
\begin{tikzpicture}[font=\Large]
\draw[-{Stealth}] (1,-2) -- (1,5) node[above] {$y$};
\draw[-{Stealth}, name path=xaxis] (-6.4,0) -- (8,0) node[right] {$x$};
\draw[domain=-6.285:6.4, name path=plot, samples=500, very thick] plot (\x+1,abs{sin(deg(\x))*4});
\end{tikzpicture}
\caption{Even extension of $f(x)=\sin x$}
  \end{subfigure} 
\end{figure}

\end{document}

任何改进都值得赞赏!

答案1

像这样?

在此处输入图片描述

(红线表示文字边框)

\documentclass{article}
\usepackage[margin=25mm]{geometry}

\usepackage[labelformat=parens,labelsep=quad,skip=3pt]{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, intersections}

\begin{document}

\begin{figure}
\tikzset{  >=Stealth,
tick/.style = {anchor=#1, font=\footnotesize, fill=white, inner xsep=1pt}
        }
    \begin{subfigure}[t]{4cm}
    \begin{tikzpicture}
% axis
\draw[->] (-1, 0.0) -- (2,0) node[below  left] {$x$};
\draw[->] ( 0,-2.2) -- (0,3) node[below right] {$y=\sin x$};
\foreach \y in {-1,1}
    \draw (2pt,2*\y) -- ++ (-4pt,0) node[tick=east] {$\y$};
% function
\draw[domain=-0:pi, samples=45, very thick] 
            plot (0.4*\x,{2*sin(deg(\x))});
\foreach \x/\i in {0, pi/\pi, pi/2\pi}
    \draw (0.4*\x,2pt) -- ++ (0,-4pt) node[tick=north] {$\i$};
    \end{tikzpicture}
\caption{$f(x)=\sin x, 0\leq x\leq 0$}
  \end{subfigure}
\hfill
    \begin{subfigure}[t]{5cm}
    \begin{tikzpicture}
% axis
\draw[->] (-1.5, 0.0) -- (3.5,0) node[below  left] {$x$};
\draw[->] ( 0.0,-2.2) -- (0.0,3) node[below right] {$y=\sin x$};
\foreach \y in {-1,1} 
    \draw (2pt,2*\y) -- ++ (-4pt,0) node[tick=east] {$\y$};
% function
\draw[domain=-pi:2*pi, samples=270, very thick] 
        plot (0.4*\x,{2*sin(deg(\x))});
\foreach \x/\i in {-pi/\pi,0, pi/\pi, 2*pi/2\pi}
    \draw (0.4*\x,2pt) -- ++ (0,-4pt) node[tick=north] {$\i$};
\end{tikzpicture}
\caption{Odd extension of $f(x)=\sin x$}
  \end{subfigure}
\hfill
    \begin{subfigure}[t]{6cm}
    \begin{tikzpicture}
% axis
\draw[->] (-2.7, 0.0) -- (3.3,0) node[below  left] {$x$};
\draw[->] ( 0.0,-2.2) -- (0.0,3) node[below right] {$y=|\sin x|$};
\foreach \y in {-1,1}
    \draw (2pt,2*\y) -- ++ (-4pt,0) node[tick=east] {$\y$};
% function
\draw[domain=-2*pi:2*pi, samples=360, very thick] 
            plot (0.4*\x,{2*abs(sin(deg(\x)))});
\foreach \x/\i in {-2*pi/2\pi,-pi/\pi,0, pi/\pi, 2*pi/2\pi}
    \draw (0.4*\x,2pt) -- ++ (0,-4pt) node[tick=north] {$\i$};
\end{tikzpicture}
\caption{Even extension of $f(x)=\sin x$}
  \end{subfigure}
\end{figure}

\end{document}

编辑: 如果您喜欢使用standalone文档类而不是article,那么您只需将上述 MWE 中的第一行替换为

\documentclass[margin=3mm,preview]{standalone}

笔记:图形相当宽,其宽度约为 16 厘米(子图形之间有空格)。这就是 MWE 中的第二行代码为 的原因\usepackage[margin=25mm]{geometry}

答案2

当然也可以用纯的来制作TikZ,但是通常用起来更容易,pgfplots并且有groupplot

€dit 1:添加了来源。
€dit 2:添加了标题。

在此处输入图片描述

%\documentclass[]{article}
%\usepackage[showframe, margin=2.5cm]{geometry}
\documentclass[margin=3pt, varwidth]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{groupplots}

\usepackage{subcaption}
\usepackage{standalone}

\begin{document}
\noindent\begin{figure}
\ifstandalone \else \centering\fi
\ifstandalone\vbox{\hbox{\fi %Because you want this in standalone.cls ===
\noindent\begin{tikzpicture}[font=\footnotesize]
\begin{groupplot}[group style={group size=3 by 1,
horizontal sep=7mm,
},
height=5.5cm, width=6.3cm,
trig format=rad, 
axis lines=center,
xlabel=$x$,
%ylabel=$y$,
axis line style={-latex},
xlabel style={anchor=north},
ylabel style={anchor=north west, inner ysep=1pt},
enlarge y limits={abs=0.5},
enlarge x limits={abs=pi/2},
xtick={-2*pi, -pi, pi, 2*pi},
xticklabels={$-2\pi$, $-\pi$, $\pi$, $2\pi$},
extra x ticks={0}, % Origin
extra x tick style={nodes={anchor=north east}, inner sep=2pt},
ymin=-1, % x-axis at same position
samples=222,
]
\nextgroupplot[ylabel={$y=\sin(x)$}, width=5cm]
\addplot[thick, domain=0:pi] {sin(x)};
\nextgroupplot[ylabel={$y=\sin(x)$}]
\addplot[thick, domain=-pi:2*pi] {sin(x)};
\nextgroupplot[ylabel={$y=|\sin(x)|$}]
\addplot[thick, domain=-2*pi:2*pi] {abs(sin(x))};
\end{groupplot}
% Captions
\tikzset{MyCaption/.style={
text width=4cm,align=left,anchor=north, draw=none, inner sep=0pt, 
}}
\node[MyCaption] at (group c1r1.south) {\subcaption{$f(x)=\sin x, 0\leq x\leq 0$}};
\node[MyCaption, text width=5cm] at (group c2r1.south) {\subcaption{Odd extension of $f(x)=\sin x$}};
\node[MyCaption, text width=5cm] at (group c3r1.south) {\subcaption{Even extension of $f(x)=\sin x$}};
\end{tikzpicture}
\ifstandalone}}\fi% =========================
\end{figure}
\end{document}

相关内容