如何在 pgfplots 中使用三角函数和刻度标签?

如何在 pgfplots 中使用三角函数和刻度标签?
\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  samples=100,
  ymax=5,legend pos=north east,
  legend style={draw=none}
]
\addplot[forget plot,cyan,domain=0.001:8] {abs(log10(x))};
\addplot[forget plot,red!70!black,domain=-2:4] {2+exp(abs(x))};
\addplot[green,thick,domain=-2:3] {exp(x)};
\addlegendentry{$e^{x}$};
\node[pin={90:$f(x)=\lvert\log x\rvert$},inner sep=0pt] 
  at (axis cs:{2,log10(2)}) {};
\node[pin={0:$f(x)=e^{x}$},inner sep=0pt] 
  at (axis cs:{1,exp(1)}) {};
\node[anchor=north west] 
  at (axis description cs:0,0.5)
  {$\lvert e^{x}\rvert$};
\end{axis}
\end{tikzpicture}

我已使用这些代码生成曲线。它向我显示如下:

输出

但是在将函数 abs(log(x)) 更改为 sin(x) 之后,如以下代码所示,

 \begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  samples=100,
  ymax=5,legend pos=north east,
  legend style={draw=none}
]
\addplot[forget plot,cyan,domain=0.001:8] {sin(x)};
\addplot[forget plot,red!70!black,domain=-2:4] {2+exp(abs(x))};
\addplot[green,thick,domain=-2:3] {exp(x)};
\addlegendentry{$e^{x}$};
\node[pin={90:$f(x)=\lvert\log x\rvert$},inner sep=0pt] 
  at (axis cs:{2,log10(2)}) {};
\node[pin={0:$f(x)=e^{x}$},inner sep=0pt] 
  at (axis cs:{1,exp(1)}) {};
\node[anchor=north west] 
  at (axis description cs:0,0.5)
  {$\lvert e^{x}\rvert$};
\end{axis}
\end{tikzpicture}

我得到的图像是

在此处输入图片描述

要求

(1)我应该用这个图表来绘制我的 sin x 图表。

(2)我的 x 轴刻度必须有,这$-\pi, \frac \pi 2 ,...$意味着我想要弧度测量刻度。

我该怎么做呢。?

答案1

除了通过写成 来修复它之外sin(x),您还可以使用 pgfplots 1.11 及更新版本从度数切换到弧度:

\pgfplotsset{trig format plots=rad}

将其添加到您的绘图后,我得到了很好地显示的正弦函数:

正弦函数输出

这首先提到特克斯世界作者pgfplots在回答“如何在 pgfplots 中使用 Grad 的三角函数参数“。

此外,你可以通过以下方式获取以刻度为单位的弧度

  xtick = {-1.5707, 0, ..., 6.28318},
  xticklabels = {$-\frac{\pi}{2}$, 0, 
    $\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$}

相关内容