我正在尝试使用 创建xbar interval
图表nodes near coords
。这适用于一些移位等,但顶部值的间隔标签缺失(或者值是多余的,取决于您如何看待它)。我该如何解决这个问题?
代码如下:
\pgfplotstableread[row sep=\\,col sep=&]{
cvar & percent \\
0.0 & 168 \\
0.005 & 188 \\
0.01 & 159 \\
0.015 & 87 \\
0.02 & 49 \\
0.025 & 0 \\
}\data
\begin{figure}[t]
\begin{tikzpicture}
\small
\begin{axis}[
yticklabel style={
/pgf/number format/.cd, fixed, fixed zerofill, precision=3, /tikz/.cd
},
scaled y ticks=false,
width=7cm,
axis lines=left,
tick label style={font=\small},
xbar interval,
grid=none,
ymax=.03,
xmax=250,
ytick=data,
yticklabel interval boundaries,
y tick label style={font=\tiny},
xmajorgrids,
nodes near coords,
every node near coord/.append style={align=right,xshift=5pt,yshift=5pt,font=\tiny},
]
\addplot[] table[y=cvar,x=percent]{\data};
\end{axis}
\end{tikzpicture}
\end{figure}
它看起来是这样的:
我希望顶部的零消失,或者在其左侧出现间隔标签。
任何帮助是极大的赞赏!
答案1
欢迎使用 TeX-SE!这将删除 0。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\pgfplotstableread[row sep=\\,col sep=&]{
cvar & percent \\
0.0 & 168 \\
0.005 & 188 \\
0.01 & 159 \\
0.015 & 87 \\
0.02 & 49 \\
0.025 & 0 \\
}\data
\begin{tikzpicture}
\begin{axis}[
yticklabel style={
/pgf/number format/.cd, fixed, fixed zerofill, precision=3, /tikz/.cd
},
scaled y ticks=false,
width=7cm,
axis lines=left,
tick label style={font=\small},
xbar interval,
grid=none,
ymax=.03,
xmax=250,
%ytick=data, %<-removed
yticklabel interval boundaries,
y tick label style={font=\tiny},
xmajorgrids,
visualization depends on={\thisrow{percent} \as \myvalue},
nodes near coords=\pgfmathtruncatemacro{\itest}{\myvalue}
\ifnum\itest>0
\pgfmathprintnumber{\myvalue}
\fi,
every node near coord/.append style={align=left,anchor=south west,font=\tiny},
]
\addplot[] table[y=cvar,x=percent]{\data};
\end{axis}
\end{tikzpicture}
\end{document}