# Create a pgfplots chart with two histograms in same plot

Is it possible to create a plot with pgfplots with the histogram of two variables in the same chart, with a different color each?

This an example code I am using, but it creates a histogram where bars are not over each other. It has dummy data.

\documentclass{minimal}

\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xtick=,
xticklabel={$\left [ \pgfmathprintnumber\tick, \pgfmathprintnumber\nexttick \right )$},
axis x line=bottom,
axis y line=left
]
\addplot+ [hist={data=x, data max=10, data min=0}] table [row sep=\\, y index=0] {
data\\
1 \\
2 \\
3 \\
4 \\
3 \\
4 \\
5 \\
6 \\
4 \\
5 \\
6 \\
3 \\
5 \\
6 \\
7 \\
8 \\
6 \\
8 \\
8 \\
8 \\
3 \\
5 \\
8 \\
};
\addplot+ [hist={data=x, data max=10, data min=0}] table [row sep=\\, y index=0] {
data\\
1 \\
2 \\
3 \\
7 \\
8 \\
3 \\
5 \\
6 \\
3 \\
8 \\
2 \\
1 \\
6 \\
3 \\
5 \\
8 \\
};
\end{axis}
\end{tikzpicture}
\end{document}

• Welcome to TeX.SE. Please make your code compileable by providing some data. We do not have your babyboom/weightgirl.dat etc. Even the problem ist not caused by the data it makes it easier to understand the problem if the code is processable. You could for example make up some dummy data. Nov 24, 2021 at 2:28
• Thanks @Roland , done. Nov 24, 2021 at 2:45
• I am not 100% sure if I understand your question right, but maybe you have the same problem as stated in tex.stackexchange.com/q/457776/95441. Could you comment on that? Nov 24, 2021 at 9:32
• My advice is that this is a bad plot because it's tough to follow. I would prefer dots connected by lines instead Dec 24, 2021 at 15:27

As mentioned by @stefan-pinnow , this discussion helped in finding that the problem was the interval keyword. The solution then was defining the chart just as ybar:

\documentclass{minimal}

\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
% xtick=,
% xticklabel={$\left [ \pgfmathprintnumber\tick, \pgfmathprintnumber\nexttick \right )$},
axis x line=bottom,
axis y line=left
]
\addplot+ [opacity=0.5, hist={data=x, data max=10, data min=0}] table [row sep=\\, y index=0] {
data\\
1 \\
2 \\
3 \\
4 \\
3 \\
4 \\
5 \\
6 \\
4 \\
5 \\
6 \\
3 \\
5 \\
6 \\
7 \\
8 \\
6 \\
8 \\
8 \\
8 \\
3 \\
5 \\
8 \\
};
\addplot+ [opacity=0.5, hist={data=x, data max=10, data min=0}] table [row sep=\\, y index=0] {
data\\
1 \\
2 \\
3 \\
7 \\
8 \\
3 \\
5 \\
6 \\
3 \\
8 \\
2 \\
1 \\
6 \\
3 \\
5 \\
8 \\
};
\end{axis}
\end{tikzpicture}
\end{document}