![如何为使用符号 y 值的 y 轴添加边距](https://linux22.com/image/446278/%E5%A6%82%E4%BD%95%E4%B8%BA%E4%BD%BF%E7%94%A8%E7%AC%A6%E5%8F%B7%20y%20%E5%80%BC%E7%9A%84%20y%20%E8%BD%B4%E6%B7%BB%E5%8A%A0%E8%BE%B9%E8%B7%9D.png)
我想使用符号 y 标签创建水平条形图。但是我:
- 希望条形图彼此相邻(即,之间没有空格)
- 为了便于阅读,我想在第一条和最后一条的下方和上方留出一些空白
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar,
axis y line=center,
axis x line=center,
bar width=4pt,
bar shift=0pt,
y=4pt,
nodes near coords, nodes near coords align={horizontal},
symbolic y coords={A,B,C,D,E,F},
]
\addplot[fill=blue] coordinates { (-58,A) (-45,B) (-43,C) };
\addplot[fill=red] coordinates {
(19,D)
(35,E)
(65,F)
};
\end{axis}
\end{tikzpicture}
\end{document}
目前看起来是这样的,任何height
规格都变成了width
规格。
答案1
像这样?
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=left,
axis y line=center,
enlargelimits={lower, 0.2},
%
xbar=11pt,
bar shift=0pt,
y=11pt,
%
nodes near coords,
nodes near coords align={horizontal},
nodes near coords style={font=\scriptsize},
%
symbolic y coords={A,B,C,D,E,F},
ytick=\empty,
]
\addplot[fill=blue] coordinates { (-58,A) (-45,B) (-43,C) };
\addplot[fill=red] coordinates { (19,D) (35,E) (65,F) };
\end{axis}
\end{tikzpicture}
\end{document}