想要使用 TikZ 在 xy 坐标中绘制函数
目前的代码
\documentclass{scrartcl}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$]
%Below the red parabola is defined
%Here the blue parabloa is defined
\addplot [
domain=0:9.5,
samples=250,
color=blue,
]
{sin(deg(x))};
\label{$sin((x))$}
\addplot [
domain=0:9.5,
samples=250,
color=blue,
]
{cos(deg(2*x))};
\addlegendentry{$cos(2x)$}
\addplot [
domain=0:9.5,
samples=250,
color=blue,
]
{sin(deg(2*x))};
\addlegendentry{$sin(2x)$}
\addplot [
domain=0:9.5,
samples=250,
color=blue,
]
{cos(deg(x))};
\addlegendentry{$cos(x)$}
\end{axis}
\end{tikzpicture}
\end{document}
如何替换 x 坐标以与 x 处的 0 对齐?如何设置每个轴的刻度?我猜我的 sin(x) 函数是错误的,是否有针对函数的特殊命令?
答案1
和pgfplots
:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[x=1cm, y=1cm,
axis lines=middle,
axis x line shift=1.2,
xlabel=\(x\),ylabel=\(y\),
x label style={anchor=north},
y label style={anchor=east},
xmin=-0.5,xmax=9.8,ymin=-1.5,ymax=1.5,
xtick={0,...,9},
clip=false,
domain=0:9.1,
smooth
]
\addplot[teal] {sin(\x r)} node[right]{$\sin x$};
\addplot[teal,dashed] {sin(2*\x r)} node[right]{$\sin (2x)$};
\addplot[magenta] {cos(\x r)} node[right]{$\cos x$};
\addplot[magenta,dashed] {cos(2*\x r)} node[right]{$\cos (2x)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
这是一个使用普通 TikZ 的简单方法。
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[smooth,domain=0:9.2]
\draw[->] (-.5,-1.5)--(10.8,-1.5) node[below=1mm]{$x$};
\draw[->] (0,-1.6)--(0,1.5) node[left]{$y$};
\foreach \i in {0,...,10}
\draw (\i,-1.5)--(\i,-1.6) node[below]{$\i$};
\foreach \j in {-1,0,1}
\draw (0,\j)--(-.1,\j) node[left]{$\j$};
\draw[teal,dashed] plot (\x,{sin(2*\x r)})
node[right]{$\sin (2x)$};
\draw[magenta,dashed] plot (\x,{cos(2*\x r)})
node[right]{$\cos (2x)$};
\draw[teal,thick] plot (\x,{sin(\x r)})
node[right]{$\sin x$};
\draw[magenta,thick] plot (\x,{cos(\x r)})
node[right]{$\cos x$};
\end{tikzpicture}
\end{document}