给出以下代码,我想修改刻度标签,使得 x 为负值时,标签位于 x 轴上方。我该如何实现?
\documentclass{report}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis x line=middle,
axis y line=middle,
width=10cm,
height=10cm,
scale only axis,
xmin=-5,
xmax=5,
xtick={-4, -2, 0, 2, 4},
xticklabels={$-4\pi$, $-2\pi$, $0$, $2\pi$, $4\pi$},
extra x ticks={-4, -2},
ymin=-1.4,
ymax=1.4,
ytick={-1, 0, 1}]
\end{axis}
\end{tikzpicture}%
\end{document}
答案1
由于您只需要两种不同的样式(而不是每个标签都不同的样式),因此您可以使用extra x ticks
负标签:
\documentclass{report}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis lines*=middle,
width=10cm,
height=10cm,
scale only axis,
xmin=-5,
xmax=5,
xtick={2, 4},
xticklabels={$2\pi$, $4\pi$},
extra x ticks={-4, -2},
extra x tick labels={$-2\pi$, $-4\pi$},
extra x tick style={
xticklabel style={yshift=0.5ex, anchor=south}
},
ymin=-1.4,
ymax=1.4,
ytick={-1, 0, 1}]
\end{axis}
\end{tikzpicture}%
\end{document}