我如何编写 tikzpicture 代码来绘制 $\sin x$ 的图形,如图所示?

我如何编写 tikzpicture 代码来绘制 $\sin x$ 的图形,如图所示?

我想绘制 sin(x) 的图形,得到x 轴如图所示。我试过了,但不是我想要的。有人能帮我提供代码吗?

\documentclass[10pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\definecolor{uuuuuu}{rgb}{0.26666666666666666,0.26666666666666666,0.26666666666666666}
\begin{center}
\begin{tikzpicture}
\begin{axis}[width=2.5in,axis equal image,
xmax=3.5,ymax=1.2,
axis lines=middle,
enlargelimits,
axis line style={shorten >=-0.25cm,shorten <=-0.25cm,latex-latex},
ticklabel style={fill=white},
xtick={0,1,2}, ytick={0,0.5,1},
xlabel=$x$,ylabel=$y$,
clip=false,]
\addplot[domain=0:2.5,mark=none,samples=200] {(sin(deg(x)))} node[fill=white, right]{$y=\sin(x)$};
\draw [fill=black] (1,0.845) circle (1.50pt);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

我知道如何绘制图中所示的点,但我不知道如何在 x 轴上获取度数。

答案1

当您希望 x 轴上有度数时,我认为使用度数进行绘制也是有意义的。因此,不要使用domain=0:2.5sin(deg(x)),而要使用domain=0:360sin(x)。执行此操作时,您需要删除axis equal image

完成后,您只需格式化刻度标签。通过使用,xticklabel您可以设置刻度标签的常规格式,因此要添加度数符号,您可以将以下内容添加到轴选项中:

xticklabel={$\pgfmathprintnumber{\tick}^{\circ}$}

\tick包含刻度处的 x 值,因此使用\pgfmathprintnumber来格式化数字,并使用正常的 添加度数符号^{\circ}。(请注意,这与 不同,末尾xticklabels有一个,您可以在其中写入所需的刻度标签列表。)s

当然,您需要更改xtick已有的设置,我将其替换为xtick distance=90每 90 度一次的设置。如果您想要更频繁的滴答声,则应增加图的宽度。

为了获取特定点的标记,一种方法是添加第二个 addplot,如下所示:

\addplot[only marks,mark=*,samples at={0,90,...,360}] {sin(x)};

这里 x 值用samples atkey 明确说明。另一种方法是使用mark repeatkey,这样你就可以在每第 n 个采样点处设置标记:

\addplot[
   domain=0:360,
   samples=181, % with domain=0:360 and 181 samples you get a sample every 2 degrees
   mark=*,
   mark repeat=45 % add a mark for every 45 sample, meaning you get a mark every 90 degreees
   ] {sin(x)} node[fill=white, right=2mm, pos=0.35]{$y=\sin(x)$};

不相关的旁注:当加载时pgfplots,您不需要tikzpgf此外,它们无论如何都会被加载pgfplots

在此处输入图片描述

\documentclass[10pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
  width=2.5in,
  xmax=380,ymax=1.2,
  axis lines=middle,
  enlargelimits,
  axis line style={shorten >=-0.25cm,shorten <=-0.25cm,latex-latex},
  ticklabel style={fill=white},
  ytick={-1,-0.5,0,0.5,1},
  xlabel=$x$,
  ylabel=$y$,
  clip=false,
  xtick distance=90,
  xticklabel={$\pgfmathprintnumber{\tick}^{\circ}$}
]

\addplot[
   domain=0:360,
   samples=181, % with domain=0:360 and 181 samples you get a sample every 2 degrees
   mark=*,
   mark repeat=45 % add a mark for every 45 sample, meaning you get a mark every 90 degreees
   ] {sin(x)} node[fill=white, right=2mm, pos=0.35]{$y=\sin(x)$};


% alternative method
%\addplot[domain=0:360,mark=none,samples=100] {sin(x)} node[fill=white, right=2mm, pos=0.35]{$y=\sin(x)$};
%\addplot[only marks,mark=*,samples at={0,90,...,360}] {sin(x)};

\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

答案2

使用 Asymptote 进行编译。渐近线.ualberta.ca

import graph;
size(10cm,5cm,false);

real sinx(real x){return sin(x);}
path g=graph(sinx,0,2pi,350);
draw(g);

draw(Label("$x$",EndPoint,black),(-1,0)--(2*pi+0.5,0),blue,Arrow);
label(Label("$x'$",BeginPoint,black),(-1,0)--(2*pi+0.5,0));
draw(Label("$y$",EndPoint,black),(0,-1.5)--(0,1.5),darkgreen,Arrow);
label(Label("$y'$",BeginPoint,black),(0,-1.5)--(0,1.5));

real marginx=0.1, marginy=0.07;

for (int i: new int[]{-1,1}){
draw(scale(0.6)*Label("$"+(string) i+"$",Relative(0)),(0,i)-(marginx,0)--(0,i)+(marginx,0));
}
for (int i=0; i<=360; i=i+30){
draw(scale(0.6)*Label((i != 0) ? "$"+(string) i+"^{\circ}$" : " ",Relative(0),black),(radians(i),0)-(0,marginy)--(radians(i),0)+(0,marginy),blue);
dot((radians(i),sinx(radians(i))), (i == 0 || i == 360) ? blue : black);
}
label("$y=\sin x$" +" on " + "$[0,2\pi]$", (radians(130),-1.5));

在此处输入图片描述

答案3

与@Torbjørn T 类似。回答(+1):

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15} % recent version is 1.17
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
    x=0.25mm,
    axis lines=middle,
    axis line style={Latex-Latex},
    xlabel=$x$, xlabel style={anchor=west},
    ylabel=$y$, ylabel style={anchor=south},
    %
    xmin=-25,   xmax=380,
    ymin=-1.25, ymax=1.25,
    ticklabel style = {font=\footnotesize},
    xtick distance=30,
    samples at={0,30,...,360},
    smooth
                ]
\addplot +[very thick] {sin(x)};
\node[above right] at (30,-1) {$y=\sin(x)$};
\end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容