我一直在尝试使用 pgfplots 和 tikz 创建该图表。
我已经记下了大部分内容,但似乎无法让轴像图片中所示那样居中,也无法让刻度标签像箱线图一样位于侧面,也无法让标签在 x 轴上以 $\pi$ 为间隔。
下面的代码给出了如下图所示的结果:
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{pgfplots} \pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}[>=Stealth]
\begin{axis}
[
grid=major,
axis lines = center,
xmin=-2*pi-0.5, xmax=3*pi+0.5,
ymin=-2, ymax=3,
xlabel=$x$, ylabel=$y$,
domain= -2*pi:3*pi
]
\plot[smooth, samples=200, thick, color=blue, <-> ]{2*sin(deg(x))+1}; %deg() used to convert radians to degrees since pgfplots wnats degrees
\end{axis}
\end{tikzpicture}
\end{document}
答案1
像这样?
代码中标记了添加到 MWE 的行:
\documentclass[border=3mm]{standalone}% <--- changed
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[>=Stealth]
\begin{axis}
[
outer axis line style={draw=gray!50},% <-- added
grid,
xmin=-2*pi-0.5, xmax=3*pi+0.5,
xtick={-6.28,-3.14,0,3.14,6.28,9.42},% <-- added
xticklabels={$-2\pi$,$-\pi$,0,$\pi$,$2\pi$,$3\pi$},% <-- added
ymin=-2, ymax=3,
xlabel=$x$, ylabel=$y$,
domain= -2*pi:3*pi
]
\addplot[smooth, samples=200, thick, color=blue, <-> ]{2*sin(deg(x))+1}; %deg() used to convert radians to degrees since pgfplots wnats degrees
\addplot[draw=none,color=blue, mark=*] coordinates {(-pi,1) (0,1) (pi,1) (2*pi,1)};% <-- added
\draw[<->] (0,-2) -- (0,3);% <-- added
\draw[<->] (-6.78,0) -- (9.92,0);% <-- added
\end{axis}
\end{tikzpicture}
\end{document}