为了绘制条形下方的负百分比和条形上方的正百分比。我使用了锚点参数,并使用了每个坐标的可视化依赖性。但是,我的锚点不起作用,因为它忽略了符号 (\myy)。
你能帮助我让它工作吗?
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{calculator}
\usepackage{xfp,xparse}
\ExplSyntaxOn
\int_new:N\l_bjprim_round_int
\keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = \l_bjprim_round_int
}
\NewDocumentCommand\percentage { O{} m }
{
\keys_set:nn {bjprim}{positions-after-comma=2,#1}
\fpeval{round(#2*100,\l_bjprim_round_int)}\%
}
\ExplSyntaxOff
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=1,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={Rendement op geïnvesteerd vermogen},
yticklabel ={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\,\%},
symbolic x coords={Vorig jaar,Huidig jaar},
xtick=data,
point meta = explicit symbolic ,
visualization depends on=y \as \myy,
nodes near coords,
node near coord style = {anchor ={\sign(\myy)*90}},
bar width = 1.5cm,
ymajorgrids =true,
]
\addplot[fill,color=red]
coordinates{(Vorig jaar,0.05)[\percentage{0.05}] (Huidig jaar,-0.05)
[\percentage{-0.05}]};
\end{axis}
\end{tikzpicture}
\end{figure}
答案1
您可以sign
直接使用。如果您更喜欢使用命令,则可以使用\pgfmathsign{<num>}
。请参阅PGF 手册,第 95.3 节,第 1034 页。
nodes near coords style = {anchor={sign(\myy)*90*(-1)}},
完整简化示例:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{xfp}
\ExplSyntaxOn
\int_new:N \l_bjprim_round_int
\keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = \l_bjprim_round_int
}
\NewDocumentCommand \percentage { O{} m }
{
\keys_set:nn {bjprim}{positions-after-comma=2, #1}
\fpeval{round(#2*100, \l_bjprim_round_int)}\%
}
\ExplSyntaxOff
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=1,
legend style={
at={(0.5,-0.15)},
anchor=north,
legend columns=-1
},
ylabel={Rendement op geïnvesteerd vermogen},
yticklabel ={%
\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\,\%
},
symbolic x coords={Vorig jaar,Huidig jaar},
xtick=data,
point meta = explicit symbolic ,
visualization depends on=y \as \myy,
nodes near coords,
nodes near coords style = {anchor={sign(\myy)*90*(-1)}},
bar width = 1.5cm,
ymajorgrids =true,
]
\addplot[fill,color=red] coordinates {
(Vorig jaar,0.05) [0.05]
(Huidig jaar,-0.05) [-0.05]
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}