均匀分布节点值

均匀分布节点值

我想将条形图上的节点值隔开,以便它们清晰可见。您对部分隐藏的节点有什么建议?我可以自动完成吗?另外,我想使用绘图坐标而不是概率的累积值。这在 pgfplots 手册的哪里?

在此处输入图片描述

\documentclass[tikz,border=12pt]{standalone}
\usepackage{pgfplots}
\usepackage{subfig}
\usepackage{tabularx}
\usepackage{verbatim}
\pgfplotsset{compat=newest}
   %\usepgfplotslibrary{external}
%\tikzexternalize
%\documentclass{article}
%\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc,
arrows,decorations.pathmorphing,
backgrounds,fit,positioning,shapes.symbols,chains}
\usetikzlibrary{decorations.pathreplacing}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
%
\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar stacked,
legend style={at={(0.5,-0.50)},
anchor=north,legend columns=-1},
ylabel={Probability},
symbolic x coords={{Direct Care}, Housekeeping, Mealtimes, {Medication Round},
Miscellaneous,{Personal Care}},
xtick=data,
ymax=1,
x tick label style={rotate=45,anchor=east},
every node near coord/.style={
      check for zero/.code={
        \pgfkeys{/pgf/fpu=true}
        \pgfmathparse{\pgfplotspointmeta-.1}
        \pgfmathfloatifflags{\pgfmathresult}{-}{
           \pgfkeys{/tikz/coordinate}
        }{}
        \pgfkeys{/pgf/fpu=false}
      }, check for zero, yshift=-5pt, font=\footnotesize},
    nodes near coords={\pgfmathprintnumber[precision=2]{\pgfplotspointmeta}},
    nodes near coords align={center}
]
\addplot+[ybar,bar width=20pt] plot coordinates {({Direct Care},0.258883249)
({Housekeeping},0.176470588)
({Mealtimes},0.19047619)
({Medication Round},0.207207207)
({Miscellaneous},0.222222222)
({Personal Care},0.615384615)
};
\addplot+[ybar,bar width=20pt] plot coordinates {({Direct Care},0.015228426)
({Housekeeping},0.235294118)
({Mealtimes},0)
({Medication Round},0.027027027)
({Miscellaneous},0)
({Personal Care},0.153846154)
};
\addplot+[ybar,bar width=20pt] plot coordinates {({Direct Care},0.152284264)
({Housekeeping},0.058823529)
({Mealtimes},0.333333333)
({Medication Round},0.351351351)
({Miscellaneous},0.180555556)
({Personal Care},0.153846154)
};
\legend{{Handwash only},{Gloves only},{Alcohol rub only}}
\end{axis}
\end{tikzpicture}
\end{document}

答案1

  • 从 1.9 版开始pgf图标签自动放置在条形图的中间
  • 绘图坐标可以与point meta=rawy
  • 我建议不要打印非常小的值的标签。例如,可以这样做:

nodes near coords=%
    {   \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
        \pgfmathtruncatemacro{\bigenough}{\pgfplotspointmeta > 0.05 ? 1 : 0}%
        \ifthenelse{\bigenough = 1}%
            {\pgfmathprintnumber[fixed,precision=2]{\pgfplotspointmeta}}%
            {}
        \pgfkeys{/pgf/fpu=false}%
    },

这将打开浮点单元库并将数字格式设置为固定格式,否则\pgfmathtruncatemacro无法使用它。然后检查该值是否大于某个任意选择的限制,此处为0.05。如果大于,则绘制它,否则不绘制。然后禁用浮点运算单元图书馆。

代码

\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{xifthen}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[   ybar stacked,
    legend style={at={(0.5,-0.50)},
    anchor=north,legend columns=-1},
    ylabel={Probability},
    symbolic x coords={{Direct Care}, Housekeeping, Mealtimes, {Medication Round}, Miscellaneous,{Personal Care}},
    xtick=data,
    ymin=0,
    ymax=1,
    point meta=rawy,
    height=8cm,
    width=10cm,
    x tick label style={rotate=45,anchor=east},
    every node near coord/.style={font=\fontsize{6}{7}\selectfont},
    nodes near coords=%
    {   \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
        \pgfmathtruncatemacro{\bigenough}{\pgfplotspointmeta > 0.05 ? 1 : 0}%
        \ifthenelse{\bigenough = 1}%
            {\pgfmathprintnumber[fixed,precision=2]{\pgfplotspointmeta}}%
            {}
        %\pgfmathprintnumber[fixed,precision=2]{\pgfplotspointmeta}%
        \pgfkeys{/pgf/fpu=false}%
    },
]
\addplot+[bar width=20pt] plot coordinates {({Direct Care},0.258883249)
({Housekeeping},0.176470588)
({Mealtimes},0.19047619)
({Medication Round},0.207207207)
({Miscellaneous},0.222222222)
({Personal Care},0.615384615)
};
\addplot+[bar width=20pt] plot coordinates {({Direct Care},0.015228426)
({Housekeeping},0.235294118)
({Mealtimes},0)
({Medication Round},0.027027027)
({Miscellaneous},0)
({Personal Care},0.153846154)
};
\addplot+[bar width=20pt] plot coordinates {({Direct Care},0.152284264)
({Housekeeping},0.058823529)
({Mealtimes},0.333333333)
({Medication Round},0.351351351)
({Miscellaneous},0.180555556)
({Personal Care},0.153846154)
};
\legend{{Handwash only},{Gloves only},{Alcohol rub only}}
\end{axis}
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容