我想使用符号 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}