我有一个 ybar 图表,其中有正条和负条以及“节点附近坐标”。我希望将正条的节点放在顶部,将负条的节点放在底部/条下方。使用以下 if-case,我可以解决负条的问题,但我找不到正确的 pgfkey 或其他任何东西来更改锚点以获得底部位置。我希望你能想出一个简单的解决方案。
非常感谢!
\documentclass{article}
\usepackage{pgfplots,lipsum}
\usepackage{tikz}
\pgfplotsset{%
/pgfplots/ybar legend/.style={
/pgfplots/legend image code/.code={%
\draw[##1,/tikz/.cd,bar width=6pt,yshift=-0.14cm,bar shift=0pt]
plot coordinates {(0cm,0.35cm)};}
}}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
\centering
\begin{axis}[small,ybar,
height=7.2cm, width=14.5cm,bar width=0.35cm,
scaled ticks=false,
y tick label style={/pgf/number format/.cd,fixed,fixed zerofill,precision=3,/tikz/.cd},
ymin=-0.016, ymax=0.056,ymajorgrids,tick align=inside,axis y line=left,
ylabel={[dimless]},
xtick=data,enlarge x limits=0.25,axis x line*=bottom,major x tick style = transparent,
symbolic x coords={$A$,$B$,$C$},
every node near coord/.append style={rotate=90,anchor=west, font=\scriptsize, xshift=-3pt, yshift=0pt},
nodes near coords={%
\pgfkeys{/pgf/fpu=true}%
\pgfmathparse{\pgfplotspointmeta<0}%
\pgfmathfloattofixed{\pgfmathresult}%
\pgfkeys{/pgf/fpu=false}%
\ifdim\pgfmathresult pt=1pt
\pgfkey{anchor={east}} % <<<<<<<<<<<<< What's the right pgfkey to set the anchor?
\else
\pgfmathprintnumber[fixed,fixed zerofill,precision=3,print sign]{\pgfplotspointmeta}
\fi
},
legend style={draw=none,draw opacity=0, at={(0.5,-0.1)},anchor=north, legend columns=5, transpose legend, font=\small,
/tikz/every even column/.append style={column sep=0.15cm}}
]
\addplot [draw=none, fill=red] coordinates {
($A$,-0.012)
($B$,+0.000)
($C$,+0.024) };
\legend{ABC}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
您正在寻找nodes near coords align
参数。我附上一个例子。
\documentclass{article}
\pagestyle{empty}
\usepackage{pgfplots,lipsum}
\usepackage{tikz}
\pgfplotsset{%
/pgfplots/ybar legend/.style={
/pgfplots/legend image code/.code={%
\draw[##1,/tikz/.cd,bar width=6pt,yshift=-0.14cm,bar shift=0pt]
plot coordinates {(0cm,0.35cm)};}
}}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
\centering
\begin{axis}[small,ybar,
height=7.2cm, width=14.5cm,bar width=0.35cm,
scaled ticks=false,
y tick label style={/pgf/number format/.cd,fixed,fixed zerofill,precision=3,/tikz/.cd},
ymin=-0.016, ymax=0.056,ymajorgrids,tick align=inside,axis y line=left,
ylabel={[dimless]},
xtick=data,enlarge x limits=0.25,axis x line*=bottom,major x tick style = transparent,
symbolic x coords={$A$,$B$,$C$},
nodes near coords align={horizontal},
every node near coord/.append style={rotate=90,
font=\scriptsize, xshift=-3pt, yshift=0pt},
nodes near coords={%
\pgfmathprintnumber[fixed,fixed zerofill,precision=3,print sign]{\pgfplotspointmeta}
},
legend style={draw=none,draw opacity=0, at={(0.5,-0.1)},anchor=north, legend columns=5, transpose legend, font=\small,
/tikz/every even column/.append style={column sep=0.15cm}}
]
\addplot [draw=none, fill=red] coordinates {
($A$,-0.012)
($B$,+0.000)
($C$,+0.024) };
\legend{ABC}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}