多列堆积条形图

多列堆积条形图

我正在尝试创建一个堆叠条形图,其中包含 3 列。我知道如何做到这一点,但无论我如何更改条形宽度或条形偏移,这些列都会相互重叠。

对于非堆叠图,一切运行良好。

代码和结果已附上。

谢谢!

\documentclass{llncs}
\usepackage{graphicx} 
\usepackage{pgfplots}
\usepackage{pgfkeys}
\usepackage{tikz}
\usetikzlibrary{patterns}
%\pgfplotsset{width=7cm,compat=1.8}

\begin{document}



\begin{tikzpicture}[
every axis/.style={ 
    ybar stacked,
    ymin=0,ymax=1500,
    symbolic x coords={
      \#5,\#6,\#7, \#8, \#15, \#16, \#18, \#19, \#22},
    bar width=3.5pt,
      legend style={font=\small, at={(0,1)},anchor=north west, draw=none},
    ylabel={size},
     ymode=log,
        log basis y={10},
    xtick=data,
    x tick label style={font=\small, 
    },
    point meta=rawy,
  },
]


\begin{axis}[bar shift=-5pt] 
\addplot coordinates {(\#5,3) (\#6,27) (\#7,81) (\#8, 243) (\#15, 16) (\#16,256) (\#18, 4) (\#19, 18)(\#22,4)};
\addplot coordinates {(\#5,3) (\#6,5) (\#7,6) (\#8, 8) (\#15, 12) (\#16, 113) (\#18, 5) (\#19, 18) (\#22,1)}; 
\end{axis}

\begin{axis}[bar shift=0pt,hide axis] 
\addplot+[] coordinates {(\#5,4) (\#6,28) (\#7,82) (\#8, 244) (\#15, 18) (\#16, 257) (\#18, 4) (\#19, 18) (\#22,8)}; 
\addplot+[pattern=north east lines] coordinates {(\#5,4) (\#6,6) (\#7,7) (\#8, 8) (\#15, 13) (\#16,113) (\#18, 5) (\#19, 18)(\#22,6)};
\end{axis}

\begin{axis}[bar shift=4pt,hide axis] 
\addplot+[pattern=north east lines] coordinates {(\#5,9) (\#6,81) (\#7,243) (\#8, 729) (\#15, 864) (\#16,1280) (\#18, 4) (\#19, 18)(\#22,1000000)}; 
\addplot+[] coordinates {(\#5,6) (\#6,8) (\#7,9) (\#8, 10) (\#15, 44) (\#16, 155) (\#18, 5) (\#19, 18) (\#22,100000)};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

据我发现,增加确实bar shift有效。请参阅代码注释以了解更改。

\documentclass{llncs}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.16} % <- 1.15 also works

\begin{document}



\begin{tikzpicture}[
/pgfplots/every axis/.style={ % <- added /pgfplots/ 
    ybar stacked,
    ymax=1500, %<- removed ymin
    symbolic x coords={
      \#5,\#6,\#7, \#8, \#15, \#16, \#18, \#19, \#22},
    bar width=3.5pt,
      legend style={font=\small, at={(0,1)},anchor=north west, draw=none},
    ylabel={size},
     ymode=log,
        log basis y={10},
    xtick=data,
    x tick label style={font=\small, 
    },
    point meta=rawy,
  },
]


\begin{axis}[bar shift=-6pt] %<- increased
\addplot coordinates {(\#5,3) (\#6,27) (\#7,81) (\#8, 243) (\#15, 16) (\#16,256) (\#18, 4) (\#19, 18)(\#22,4)};
\addplot coordinates {(\#5,3) (\#6,5) (\#7,6) (\#8, 8) (\#15, 12) (\#16, 113) (\#18, 5) (\#19, 18) (\#22,1)}; 
\end{axis}

\begin{axis}[bar shift=0pt,hide axis] 
\addplot+[] coordinates {(\#5,4) (\#6,28) (\#7,82) (\#8, 244) (\#15, 18) (\#16, 257) (\#18, 4) (\#19, 18) (\#22,8)}; 
\addplot+[pattern=north east lines] coordinates {(\#5,4) (\#6,6) (\#7,7) (\#8, 8) (\#15, 13) (\#16,113) (\#18, 5) (\#19, 18)(\#22,6)};
\end{axis}

\begin{axis}[bar shift=6pt,hide axis] %<- increased
\addplot+[pattern=north east lines] coordinates {(\#5,9) (\#6,81) (\#7,243) (\#8, 729) (\#15, 864) (\#16,1280) (\#18, 4) (\#19, 18)(\#22,1000000)}; 
\addplot+[] coordinates {(\#5,6) (\#6,8) (\#7,9) (\#8, 10) (\#15, 44) (\#16, 155) (\#18, 5) (\#19, 18) (\#22,100000)};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容