如何顯示整個長條圖並在兩個方向(頂部和底部)上稍微延伸 y 軸?
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar,
xmin = 0,
y = 35pt,
bar width= 2em,
axis x line = none,
axis y line = left,
tickwidth = 0pt,
symbolic y coords = {A,B,C,D},
nodes near coords,
]
\addplot coordinates { (45,A) (55,B) (40,C) (50,D) };
\end{axis}
\end{tikzpicture}
\end{document}
答案1
是的。您可以使用 擴大 y 限制enlarge y limits
。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar,
xmin = 0,
y = 35pt,
bar width= 2em,
axis x line = none,
axis y line = left,
tickwidth = 0pt,
symbolic y coords = {A,B,C,D},
nodes near coords,enlarge y limits=0.2
]
\addplot coordinates { (45,A) (55,B) (40,C) (50,D) };
\end{axis}
\end{tikzpicture}
\end{document}