如何通过 pgfplots 增加条形图中条形之间的间距?
\documentclass{minimal}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
every axis plot post/.style={/pgf/number format/fixed},
ybar,
x=3cm,
ymin=-0.1,
ymax=12,
xtick=data,
enlarge x limits=0.2,
bar width=15pt,
symbolic x coords={A,B,C},
nodes near coords,
axis lines*=left,
]
\addplot coordinates {(A,2) (B,0.5) (C,10)};
\addplot coordinates {(A,20) (B,30) (C,1)};
\addplot coordinates {(A,6) (B,11) (C,7)};
\end{axis}
\end{tikzpicture}
\end{document}
我想增加条形图 (A,2)、(A,20)、(A,6) 之间的间距(B 和 C 类似)。
另外要注意的是:我该如何处理太大而无法显示的值?(例如(B,30))
答案1
您可以使用可选参数来设置条形之间的间距ybar
(默认值为2pt
)。
为了指示某些数据值超出轴范围,您可以使用 将值剪切到略大于轴限制的某个级别restrict y to domain*=0:<value>
,并确保设置以使条形图可以超出轴。您可以通过设置clip=false
使原始未剪切的 y 值可用于,并使用键绘制一条表示中断的线:nodes near coords
visualization depends on=rawy \as \rawy
after end axis/.code
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
every axis plot post/.style={/pgf/number format/fixed},
ybar=5pt,
bar width=12pt,
x=3cm,
ymin=0,
axis on top,
ymax=12,
xtick=data,
enlarge x limits=0.2,
symbolic x coords={A,B,C},
restrict y to domain*=0:14, % Cut values off at 14
visualization depends on=rawy\as\rawy, % Save the unclipped values
after end axis/.code={ % Draw line indicating break
\draw [ultra thick, white, decoration={snake, amplitude=1pt}, decorate] (rel axis cs:0,1.05) -- (rel axis cs:1,1.05);
},
nodes near coords={%
\pgfmathprintnumber{\rawy}% Print unclipped values
},
axis lines*=left,
clip=false
]
\addplot coordinates {(A,2) (B,0.5) (C,10)};
\addplot coordinates {(A,20) (B,30) (C,1)};
\addplot coordinates {(A,6) (B,11) (C,7)};
\end{axis}
\end{tikzpicture}
\end{document}