我有以下图表
我需要在 y 轴刻度上以及在条形图顶部写出十进制数。我在这里找到了一些其他问题,它们建议使用y tick label style={/pgf/number format/fixed}
,但这对我来说毫无用处,而scaled ticks=false
,这对轴有用,但也产生了一些奇怪的效果,对条形图不起作用。
有没有一种可靠的方法可以让我完整地写出轴和节点上的数字(0.0051 和 0.018)?
母语:
\documentclass{article}
\usepackage[dvipsnames]{xcolor} %
\usepackage{pgfplots} %
\begin{document}
\begin{tikzpicture}
\pgfplotsset{width=9 cm, height=9cm}
\begin{axis} [
symbolic x coords={0, last year, this year},
xtick={last year, this year},
% scaled ticks=false,
axis lines*=left,
ymajorgrids = true, %shows the y grid
ymin=0,
% y tick label style={/pgf/number format/fixed},
bar width=12.5mm,
ybar=-1cm,
enlarge x limits={abs=2.33cm},
nodes near coords,
every node near coord/.append style={color=black},
]
\addplot [Red,fill=Red]
coordinates{ (last year,0.0051) (this year,0.018) } ;
\end{axis}
\end{tikzpicture}
\end{document}
谢谢
答案1
要设置轴和条的数字格式,我们可以使用
\pgfkeys{/pgf/number format/fixed,/pgf/number format/precision=4}
代码
\documentclass{article}
\usepackage[dvipsnames]{xcolor} %
\usepackage{pgfplots} %
\pgfplotsset{compat=1.18}%<-- added
\begin{document}
\begin{tikzpicture}
\pgfplotsset{width=9 cm, height=9cm}
\pgfkeys{
/pgf/number format/fixed,
/pgf/number format/precision=4,
}%<-- this line
\begin{axis} [
symbolic x coords={0, last year, this year},
xtick={last year, this year},
scaled ticks=false,
axis lines*=left,
ymajorgrids = true, %shows the y grid
ymin=0,
% y tick label style={/pgf/number format/fixed,/pgf/number format/precision=4},
bar width=12.5mm,
ybar=-1cm,
enlarge x limits={abs=2.33cm},
nodes near coords,
every node near coord/.append style={color=black},
]
\addplot [Red,fill=Red]
coordinates{ (last year,0.0051) (this year,0.018) } ;
\end{axis}
\end{tikzpicture}
\end{document}