向堆叠条形图中添加单个条形图

向堆叠条形图中添加单个条形图

为了显示三个测量值与单个测量值之间的差异,我需要将常规条形图与堆叠条形图结合起来。但是,一旦我使用堆叠条形图,单个数据就会发生偏移:

图片

剧情如下:

\begin{tikzpicture}
\begin{axis}[
    xbar stacked,
    xmin=0,
  ]

  \addplot coordinates {
    ({0.394240+1.067584}, 0)
    ({0.614592+1.247520}, 1)
    ({1.862848+1.615360}, 2)
    ({4.025184+2.461248}, 3)
  };

  \addplot coordinates {
    ({0.333824+0.553664}, 0)
    ({0.602816+1.236160}, 1)
    ({1.719200+0.600512}, 2)
    ({3.526432+1.282208}, 3)
  };

  \addplot coordinates {
    ({0.334912+0.522688}, 0)
    ({0.642980+0.701760}, 1)
    ({1.873760+0.997440}, 2)
    ({3.856416+1.302432}, 3)
  };

  \addplot coordinates {
    (0.630336, 0.5)
    (1.925728, 1.5)
    (6.348640, 2.5)
    (15.193056, 3.5)
  };
\end{axis}
\end{tikzpicture}

是否可以将橙色条移回 0?

答案1

这是一个可能的解决方案。由于单个条形图是 ,xbar无需堆叠。因此,此解决方案建议axis使用两次环境,一次用于 ,xbar stacked一次用于xbar。最终结果有一组浮动黑条,用于演示目的。只需删除addplot箭头 <--- 指示的命令即可摆脱它们。

在此处输入图片描述

代码

\documentclass[border=10pt]{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xbar stacked,
    xmin=0,xmax=40, ymax=4
  ]
  \addplot coordinates {
    ({0.394240+1.067584}, 0)
    ({0.614592+1.247520}, 1)
    ({1.862848+1.615360}, 2)
    ({4.025184+2.461248}, 3)
  };

  \addplot coordinates {
    ({0.333824+0.553664}, 0)
    ({0.602816+1.236160}, 1)
    ({1.719200+0.600512}, 2)
    ({3.526432+1.282208}, 3)
  };

  \addplot coordinates {
    ({0.334912+0.522688}, 0)
    ({0.642980+0.701760}, 1)
    ({1.873760+0.997440}, 2)
    ({3.856416+1.302432}, 3)
  };
%   Remove  this plot will remove the black bar
  \addplot coordinates { %<---
    (0.630336, 0.5)      %
    (1.925728, 1.5)      %
    (6.348640, 2.5)      %
    (15.193056, 3.5) };  %<---
\end{axis}

\begin{axis}[
    xbar, 
    axis y line=none,
    xmin=0,xmax=40, ymax=4,
]
  \addplot[fill=orange]  coordinates {
    (0,0)
    (0.630336, 0.5) 
    (1.925728, 1.5)
    (6.348640, 2.5)
    (15.193056, 3.5)};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容