如何指定节点附近坐标的小数位数?

如何指定节点附近坐标的小数位数?

有没有办法指定node near coords标签应使用 1 位小数而不是 2 位小数?例如,在下面的代码中,新加坡打印的是 8.1(正确),但尼日尔打印的是 46.12(错误),而我希望它是 46.1(正确)。

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xlabel={Birth rate (births/1,000 population)},
    symbolic y coords={Niger,United States,Singapore},
    ytick=data,
    xbar,
    nodes near coords, nodes near coords align={horizontal}]

  \addplot coordinates {
    (46.12,Niger)
    (13.42,United States)
    (8.10,Singapore)
  };

\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

使用

nodes near coords style={/pgf/number format/.cd,precision=1}

或者

nodes near coords style={/pgf/number format/.cd,fixed zerofill,precision=1}

具有fixed zerofill像 的值4.00将被打印为4.0

在此处输入图片描述

代码:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xlabel={Birth rate (births/1,000 population)},
    symbolic y coords={Niger,United States,Singapore},
    ytick=data,
    xbar,
    nodes near coords,
    nodes near coords style={/pgf/number format/.cd,fixed zerofill,precision=1},%<- added
    enlarge x limits={upper,.15}% <-added
    ]
  \addplot coordinates {
    (46.12,Niger)
    (13.42,United States)
    (8.10,Singapore)
  };
\end{axis}
\end{tikzpicture}
\end{document}

相关内容