如何在 pgfplots 上配置轴?与此类似。
\documentclass{article}
\usepackage{tikz,pgfplots}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[>=latex]
%x axis
\draw[->] (-5,0) -- (5,0) node[below] {$x$};
\foreach \x in {-4,...,-1,1,2,...,4}
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
%y axis
\draw[->] (0,-5) -- (0,5) node[left] {$y$};
\foreach \y in {-4,...,-1,1,2,...,4}
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\node[below left] at (0,0) {\footnotesize $0$};
\end{tikzpicture}
\end{document}
答案1
\documentclass{article}
\usepackage{tikz,pgfplots}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[
axis x line=center,
axis y line=center,
xtick={-5,-4,...,5},
ytick={-5,-4,...,5},
xlabel={$x$},
ylabel={$y$},
xlabel style={below right},
ylabel style={above left},
xmin=-5.5,
xmax=5.5,
ymin=-5.5,
ymax=5.5]
\addplot [mark=none,domain=-4:4] {x};
\end{axis}
\end{tikzpicture}
\end{document}