下面的 latex 代码对条形图的值进行了四舍五入。我需要这些值保持完整。例如,0.8197 不应四舍五入为 0.82。
\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={baseline, seq\_1, seq\_2, seq\_3, seq\_4},
xtick=data,
nodes near coords,
]
\addplot[ybar,fill=blue] coordinates {
(baseline, 0.8197)
(seq\_1, 0.7006)
(seq\_2, 0.5134)
(seq\_3, 0.8314)
(seq\_4, 0.8449)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
添加/pgf/number format/.cd,fixed,precision=4
选项axis
以使用具有 4 位精度的固定数字格式。请参阅pgfmanual
v3.0.1a 第 92 节 p946
编辑
正如 Peter Grill 所指出的,/pgf/number format/.cd,fixed,precision=4
作用于整个轴,因此倾向于every node near coord/.append style={/pgf/number format/.cd, fixed,precision=4}
仅将此特定格式应用于条形图顶部的数字。
\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={baseline, seq\_1, seq\_2, seq\_3, seq\_4},
xtick=data,
nodes near coords,
every node near coord/.append style={/pgf/number format/.cd, fixed,precision=4}
]
\addplot[ybar,fill=blue] coordinates {
(baseline, 0.8197)
(seq\_1, 0.7006)
(seq\_2, 0.5134)
(seq\_3, 0.8314)
(seq\_4, 0.8449)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
默认情况下nodes near coords
打印\pgfmathprintnumber\pgfplotspointmeta
。你可以告诉它precision=4
使用
nodes near coords={\pgfmathprintnumber[precision=4]{\pgfplotspointmeta}}
而不仅仅是
nodes near coords
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={baseline, seq\_1, seq\_2, seq\_3, seq\_4},
xtick=data,
nodes near coords={\pgfmathprintnumber[precision=4]{\pgfplotspointmeta}},
]
\addplot[ybar,fill=blue] coordinates {
(baseline, 0.8197)
(seq\_1, 0.7006)
(seq\_2, 0.5134)
(seq\_3, 0.8314)
(seq\_4, 0.8449)
};
\end{axis}
\end{tikzpicture}
\end{document}