# How to set a fixed bin width for pgfplots histogram?

I want to plot multiple histograms next to each other using pgfplots, and I found the nice statistics library.

However, I can't find how I can set the bin width at a fixed size instead of the amount of bins. The amount of bins is very fragile, a different range of the data has quite an effect on the width of a bin.

I've adapted the example from the documentation to show the "problem".

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ybar]
table[row sep=\\,y index=0] {
data\\
%1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};
\end{axis}
\end{tikzpicture}

\end{document}


I know how to get the y axis the same range, the question is how I can get the bin width to be stable?

• Do you mean something like this hist={bins=3, data min=0, data max=10}? – Benjamin Apr 4 '16 at 15:04
• Indeed, except that would only work if the min and max are exactly the same over the data sets. – Davy Landman Apr 5 '16 at 5:50
• I now understand you comment better, the actual problem is that the data is read from a data file, and i do not know the min max up front. – Davy Landman Apr 5 '16 at 12:08
• Using rectangular bins to make your histograms will always be very sensitive to your input. A better approach would be to use Kernel density estimations en.wikipedia.org/wiki/Kernel_density_estimation, but this can not be done using pgfplots properly. Although there is a function pgfplots/hist/density, this still makes use of the dreaded bar plot. Regarding your question, I don't think this is possible in pgfplots, maybe process the data beforehand? – Roald Sep 18 '16 at 20:01