![将轴刻度添加到绘图中](https://linux22.com/image/405980/%E5%B0%86%E8%BD%B4%E5%88%BB%E5%BA%A6%E6%B7%BB%E5%8A%A0%E5%88%B0%E7%BB%98%E5%9B%BE%E4%B8%AD.png)
我需要给这个图形添加一个刻度,在 x 轴上从 -3 到 3,每次增加 1 个单位,在 y 轴上从 -1.5 到 1.5,每次增加 0.5 个单位。代码如下:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=-3:3, range=-3:3]
\draw[very thin,color=gray] (-3,+3);
\draw[->] (-3,0) -- (3,0) node[right] {$x$};
\draw[->] (0,-1.5) -- (0,1.5) node[above] {$y$};
\draw[color=red] plot (\x,1) node[right] {$f(x) =1$};
\draw[color=blue] plot (\x,{cos(\x r)}) node[right] {$f(x) = \cos x$};
\draw[color=blue] plot (\x, {1 -\x*\x/2}) node[right] {$f(x) = 1- \frac{x^2}{2}$};
\end{tikzpicture}
\end{document}
答案1
添加以下行:
\foreach \x in {-3,...,3}{0 \draw (\x,0) node {$|$} node [yshift=-0.3cm]{$\x$};}
\foreach \x in {-1.5,-1,-0.5,0,0.5,1,1.5}{0 \draw (0,\x) node {$-$} node [xshift=-0.3cm]{$\x$};}
你可以改变 x 和 y 的偏移量来让它看起来更好