# stacking histograms in pgfplots

The stacking is not working correctly. This is what I get:

This might be a problem of different bins, but even restricting them is not working (slightly related questions about common bins are here, here, and here. Yet, they have not been helpful).

This is my code from above:

\documentclass{article}
\usepackage[]{graphicx}
\usepackage{tikz,pgfplots,pgfplotstable}
\usetikzlibrary{patterns}
\usepgfplotslibrary{statistics}
\pagestyle{empty}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar stacked,normalsize,width=10cm,enlargelimits=0,bar width=0.6cm,
ytick={0,200,400,600,800,1000},
xtick style={draw=none},
xtick={0,20,...,100},
]
\addplot+[hist={bins=25,data min=-2,data max=102}] table [x,y index=0] {w1.csv};
\addplot+[hist={bins=25,data min=-2,data max=102}] table [x,y index=0] {w2.csv};
\addplot+[hist={bins=25,data min=-2,data max=102}] table [x,y index=0] {w3.csv};
\end{axis}
\end{tikzpicture}

\end{document}


My data (numbers) comes from three files, each with one column. Files are very large. Therefore, I will use a small example that shows the problem clearly.

This is the example code:

\documentclass{article}
\usepackage[]{graphicx}
\usepackage{tikz,pgfplots,pgfplotstable}
\usetikzlibrary{patterns}
\usepgfplotslibrary{statistics}
\pagestyle{empty}
\usepackage{filecontents}

\begin{filecontents}{w.csv}
1.4
1.1
2.2
3.3
4.1
4.5
6.1
6.6
6.6
7.4
8.1
8.3
9.1
9.1
9.1
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}[ybar stacked,xtick={0,1,...,10},ytick={0,1,...,6}]
\addplot+[hist={bins=10,data min=0,data max=10}] table [x,y index=0] {w.csv};
\addplot+[hist={bins=10,data min=0,data max=10}] table [x,y index=0] {w.csv};
\end{axis}
\end{tikzpicture}

\end{document}


Notice that, for simplicity, I'm using the same file. In consequence, the pattern of stacked histograms should be trivial. Yet, this is the output:

• This seems to be a bug. Updating to version 1.13 of pgfplots does not solve the issue. – luchonacho Jan 28 '16 at 18:19

I had the same issue and found no other thread with this topic. As this is not resolved in pgfplots 1.15 my solution was to simply "stack" the files myself, e.g.

data-1.txt = [ 1 2 3 4 5 ]
data-2.txt = [ 1 3 3 4 5 ]
data-1-and-2.txt = [ 1 2 3 4 5 1 3 3 4 5 ]


then plot data-1-and-2.txt first and data-1.txt after that and omit

ybar stacked


in the axis environment

\begin{tikzpicture}
\begin{axis}[xtick={0,1,...,10},ytick={0,1,...,6}]
\addplot+[hist={bins=10,data min=0,data max=10}] table [x,y index=0]  {data-1-and-2.txt};
\addplot+[hist={bins=10,data min=0,data max=10}] table [x,y index=0] {data-1.txt};
\end{axis}
\end{tikzpicture}

• Welcome to TeX.SX! Could you add a minimal running code with preamble, \documentclass and \end{document}. – Bobyandbob Aug 8 '17 at 12:26
• Yes, and add a picture of the result please. – luchonacho Aug 8 '17 at 19:22