\documentclass{standalone}
\usepackage{tikz,pgf,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar, xmin=0,
xlabel={\textbf{Unit Weight (in $\mathbf{kN/m^3}$)}},
symbolic y coords={%
{Wood (Oak)},
{Wood (Pine)},
{Water},
{Steel},
{Polystyrene Foam},
{Limestone},
{Glass},
{Concrete},
{Brick},
Aluminium},
ytick=data,
nodes near coords,
nodes near coords align={horizontal},
ytick=data, ylabel={\textbf{Building/Construction Materials}},width=13.5cm,height=8cm
]
\addplot[xbar,fill=red!60] coordinates {
(27.0,{Aluminium})
(19.3,{Brick})
(24.0,{Concrete})
(24.2,{Glass})
(16.2,{Limestone})
(0.3,{Polystyrene Foam})
(76.8,{Steel})
(9.8,{Water})
(5.0,{Wood (Pine)})
(7.1,{Wood (Oak)})};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
使用 1.12 版本,我得到0.3
。因此您可能需要更新您的pgfplots
软件包。要将 加零5
,您可以使用以下命令:
every node near coord/.append style={/pgf/number format/precision=1,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill},
在axis
选项中。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12} %% better use this and 1.12 may be your version
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar, xmin=0,
xlabel={\textbf{Unit Weight (in $\mathbf{kN/m^3}$)}},
symbolic y coords={%
{Wood (Oak)},
{Wood (Pine)},
{Water},
{Steel},
{Polystyrene Foam},
{Limestone},
{Glass},
{Concrete},
{Brick},
Aluminium},
ytick=data,
nodes near coords,
every node near coord/.append style={/pgf/number format/precision=1,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,},
nodes near coords align={horizontal},
ytick=data, ylabel={\textbf{Building/Construction Materials}},width=13.5cm,height=8cm
]
\addplot[xbar,fill=red!60] coordinates {
(27.0,{Aluminium})
(19.3,{Brick})
(24.0,{Concrete})
(24.2,{Glass})
(16.2,{Limestone})
(0.3,{Polystyrene Foam})
(76.8,{Steel})
(9.8,{Water})
(5.0,{Wood (Pine)})
(7.1,{Wood (Oak)})};
\end{axis}
\end{tikzpicture}
\end{document}
如果您使用0.03
而不是0.3
那么您必须使用/pgf/number format/precision=2
,否则您将得到 0.0 而不是 0.03。