我有以下圖表
我需要在 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}