从这个问题开始,我如何制作 tikzpicture 代码来绘制 $\sin x$ 的图形,如图所示?,考虑同一张图像,我们可以看到轴有不同的颜色:-x
轴为青色,函数为洋红色,-轴sin(x)
为绿松石y
。
以@Torbjørn T. 的 MWE 为例:
\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}
或者@Zarko,是否可以用不同的模式给轴上色?
\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}
我已经看到了手动的但我没有找到与我的问题相关的任何内容。
答案1
好吧,@Sebastiano 在他的问题中提到了我...考虑到@ZhiyuanLck 的评论,从我的答案中使用代码变成:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x=0.25mm,
axis lines=middle,
separate axis lines,
x axis line style={Latex-Latex, blue},
y axis line style={Latex-Latex, red},
axis on top,
%
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, inner ysep=2pt,
fill=white, fill opacity=0.5, text opacity=1},
xtick distance=30,
samples at={0,30,...,360},
xticklabel={\pgfmathprintnumber{\tick}\si{\degree}}, % <---
smooth
]
\addplot +[very thick] {sin(x)};
\node[above right] at (30,-1) {$y=\sin(x)$};
\end{axis}
\end{tikzpicture}
\end{document}
编译结果为:
笔记:所有功劳都归于致远集团。