我正在尝试制作一个条形图,其中始终有一个条形与多个条形配对。出现了两个问题——首先,第一个条形被认为有 6 个其他条形伙伴,并被推到左边。我知道这个问题已经得到解答这里,但还有更多。第二个问题是,假设解决方案(第二个 MWE),我会在组之间浪费大量空白。关于如何实现条形组和图边缘之间的均匀间距,有什么想法吗?如果需要,肮脏的黑客和手动方法也可以。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
width=12cm,
xmin=0,xmax=2,
enlarge x limits=0.3,
]
\addplot coordinates {
(0,2.5) };
\addplot coordinates {
(1,5) (2,7) };
\addplot coordinates {
(1,4) (2,10) };
\addplot coordinates {
(1,5) (2,8) };
\addplot coordinates {
(1,2) (2,6) };
\addplot coordinates {
(1,8) (2,10) };
\addplot coordinates {
(1,7) (2,4) };
\end{axis}
\end{tikzpicture}
\end{document}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{
% #1: index in the group(0,1,2,...)
% #2: number of plots of that group
bar group size/.style 2 args={
/pgf/bar shift={%
% total width = n*w + (n-1)*skip
% -> subtract half for centering
-0.5*(#2*\pgfplotbarwidth + (#2-1)*\pgfkeysvalueof{/pgfplots/bar group skip}) +
% the '0.5*w' is for centering
(.5+#1)*\pgfplotbarwidth + #1*\pgfkeysvalueof{/pgfplots/bar group skip}},%
},
bar group skip/.initial=0pt,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
plot1/.style={fill=red!20},
plot2/.style={fill=blue!20},
ybar,
width=12cm,
xmin=0,xmax=2,
enlarge x limits=0.2,
]
\addplot [bar group size={5}{6}] coordinates {
(0,2.5) };
\addplot [plot1,bar group size={0}{6}] coordinates {
(1,5) (2,7) };
\addplot [plot2,bar group size={1}{6}] coordinates {
(1,4) (2,10) };
\addplot [plot1,bar group size={2}{6}] coordinates {
(1,5) (2,8) };
\addplot [plot2,bar group size={3}{6}] coordinates {
(1,2) (2,6) };
\addplot [plot1,bar group size={4}{6}] coordinates {
(1,8) (2,10) };
\addplot [plot2,bar group size={5}{6}] coordinates {
(1,7) (2,4) };
\end{axis}
\end{tikzpicture}
\end{document}
答案1
为此,我将使用两个相对于彼此定位的独立轴。
使用一些样式,您可以保持非常易于维护。在下面的代码中,我定义了键,因此您只需将键应用于kynes left axis
单个条形的轴,以及kynes right axis
应用于条形组的轴。您可以使用 设置条形组之间的间隙以及轴和条形之间的间隙/pgfplots/group gap
。
该图是使用以下方法生成的:
\begin{tikzpicture}
\pgfplotsset{
group size=6,
group gap=0.5cm,
/pgf/bar width=0.35cm
}
\begin{axis}[
kynes left axis,
]
\addplot [fill=red, draw=black] coordinates {(0,2.5)};
\end{axis}
\begin{axis}[
kynes right axis
]
\addplot [plot1] coordinates {
(1,5) (2,7) };
\addplot [plot2] coordinates {
(1,4) (2,10) };
\addplot [plot1] coordinates {
(1,5) (2,8) };
\addplot [plot2] coordinates {
(1,2) (2,6) };
\addplot [plot1] coordinates {
(1,8) (2,10) };
\addplot [plot2] coordinates {
(1,7) (2,4) };
\end{axis}
\end{tikzpicture}
完整代码如下:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotsset{
group gap/.initial=0.75cm,
group size/.initial=6,
kynes axis/.style={
plot1/.style={fill=red!20},
plot2/.style={fill=blue!20},
ybar=0pt, % No gap between bars
ymin=0, ymax=11,
},
kynes left axis/.style={
kynes axis,
name=kynes left axis,
axis y line*=left,
clip=false,
xmin=-0.5, xmax=0.5,
xtick=data,
x=\pgfkeysvalueof{/pgf/bar width},
enlarge x limits={abs=\pgfkeysvalueof{/pgfplots/group gap}, lower}
},
kynes right axis/.style={
kynes axis,
at=(kynes left axis.east),
anchor=west,
axis y line*=right,
xtick=data,
ytick=\empty,
x=\pgfkeysvalueof{/pgfplots/group size}*\pgfkeysvalueof{/pgf/bar width}+\pgfkeysvalueof{/pgfplots/group gap},
enlarge x limits={abs=\pgfkeysvalueof{/pgfplots/group size}/2*\pgfkeysvalueof{/pgf/bar width}+\pgfkeysvalueof{/pgfplots/group gap}},
}
}
\begin{tikzpicture}
\pgfplotsset{
group size=6,
group gap=0.5cm,
/pgf/bar width=0.35cm
}
\begin{axis}[
kynes left axis,
]
\addplot [fill=red, draw=black] coordinates {(0,2.5)};
\end{axis}
\begin{axis}[
kynes right axis
]
\addplot [plot1] coordinates {
(1,5) (2,7) };
\addplot [plot2] coordinates {
(1,4) (2,10) };
\addplot [plot1] coordinates {
(1,5) (2,8) };
\addplot [plot2] coordinates {
(1,2) (2,6) };
\addplot [plot1] coordinates {
(1,8) (2,10) };
\addplot [plot2] coordinates {
(1,7) (2,4) };
\end{axis}
\end{tikzpicture}
\end{document}