使用ycomb
低于 0 的图意味着我的一个刻度标签被线遮住了。如何将这个单独的刻度移动到 x 轴上方?有没有不用选项就可以做到这一点的方法extra x ticks
?
另外,我怎样才能删除坐标附近值为 0 的节点?
谢谢
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
tick style={draw=none},
yticklabels=none,
axis x line=middle,
axis y line=middle,
xmin=-2,
xmax=4.5,
xlabel=$n$,
ymin=-0.5,
ymax=2.5,
ylabel={$x[n]$},
nodes near coords,
nodes near coords align={south east}
]
\addplot+[ycomb,thick,mark=o] plot coordinates
{(-2,0) (-1,-0.5) (0,1) (1,2) (2,0) (3,1) (4,0)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
如果它只有一个刻度标签,你可以使用
x tick label style={yshift={(\ticknum==2)*2em}}
或者
x tick label style={yshift={(\tick==-1)*2em}}
您还可以添加以下内容来删除nodes near coords
with 值0
:
nodes near coords={%
\pgfmathprintnumberto[assume math mode]{\pgfplotspointmeta}{\nodenearcoord}%
\pgfmathparse{\nodenearcoord==0?:\nodenearcoord}%
$\pgfmathresult$%
}
代码:
\documentclass{standalone}
\usepackage{scrextend}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
tick style={draw=none},
yticklabels=none,
axis x line=middle,
axis y line=middle,
xmin=-2,
xmax=4.5,
xlabel=$n$,
ymin=-0.5,
ymax=2.5,
ylabel={$x[n]$},
nodes near coords={%
\pgfmathprintnumberto[assume math mode]{\pgfplotspointmeta}{\nodenearcoord}%
\pgfmathparse{\nodenearcoord==0?:\nodenearcoord}%
$\pgfmathresult$%
},
nodes near coords align={south east},
x tick label style={yshift={(\tick==-1)*2em}}
]
\addplot+[ycomb,thick,mark=o] plot coordinates
{(-2,0) (-1,-0.5) (0,1) (1,2) (2,0) (3,1) (4,0)};
\end{axis}
\end{tikzpicture}
\end{document}