| | .. _astropy-visualization-hist: |
| |
|
| | *********************** |
| | Choosing Histogram Bins |
| | *********************** |
| |
|
| | The :mod:`astropy.visualization` module provides the |
| | :func:`~astropy.visualization.hist` function, which is a generalization of |
| | matplotlib |
| | of histogram bins. For computing bins without the accompanying plot, see |
| | :func:`astropy.stats.histogram`. |
| |
|
| | As a motivation for this, consider the following two histograms, which are |
| | constructed from the same underlying set of 5000 points, the first with |
| | matplotlib |
| | 200 bins: |
| |
|
| | .. plot:: |
| | :align: center |
| |
|
| | import numpy as np |
| | import matplotlib.pyplot as plt |
| |
|
| | # generate some complicated data |
| | rng = np.random.RandomState(0) |
| | t = np.concatenate([-5 + 1.8 * rng.standard_cauchy(500), |
| | -4 + 0.8 * rng.standard_cauchy(2000), |
| | -1 + 0.3 * rng.standard_cauchy(500), |
| | 2 + 0.8 * rng.standard_cauchy(1000), |
| | 4 + 1.5 * rng.standard_cauchy(1000)]) |
| |
|
| | # truncate to a reasonable range |
| | t = t[(t > -15) & (t < 15)] |
| |
|
| | # draw histograms with two different bin widths |
| | fig, ax = plt.subplots(1, 2, figsize=(10, 4)) |
| |
|
| | fig.subplots_adjust(left=0.1, right=0.95, bottom=0.15) |
| | for i, bins in enumerate([10, 200]): |
| | ax[i].hist(t, bins=bins, histtype= |
| | ax[i].set_xlabel( |
| | ax[i].set_ylabel( |
| | ax[i].set_title( |
| | fontdict=dict(family= |
| |
|
| | Upon visual inspection, it is clear that each of these choices is suboptimal: |
| | with 10 bins, the fine structure of the data distribution is lost, while with |
| | 200 bins, heights of individual bins are affected by sampling error. |
| | The tried-and-true method employed by most scientists is a trial and error |
| | approach that attempts to find a suitable midpoint between these. |
| |
|
| | Astropy |
| | providing several methods of automatically tuning the histogram bin size. |
| | It has a syntax identical to matplotlib |
| | exception of the ``bins`` parameter, which allows specification of one of |
| | four different methods for automatic bin selection. These methods are |
| | implemented in :func:`astropy.stats.histogram`, which has a similar syntax |
| | to the ``np.histogram`` function. |
| |
|
| | Normal Reference Rules |
| | ====================== |
| | The simplest methods of tuning the number of bins are the normal reference |
| | rules due to Scott (implemented in :func:`~astropy.stats.scott_bin_width`) and |
| | Freedman & Diaconis (implemented in :func:`~astropy.stats.freedman_bin_width`). |
| | These rules proceed by assuming the data is close to normally-distributed, and |
| | applying a rule-of-thumb intended to minimize the difference between the |
| | histogram and the underlying distribution of data. |
| |
|
| | The following figure shows the results of these two rules on the above dataset: |
| |
|
| | .. plot:: |
| | :align: center |
| |
|
| | import numpy as np |
| | import matplotlib.pyplot as plt |
| | from astropy.visualization import hist |
| |
|
| | # generate some complicated data |
| | rng = np.random.RandomState(0) |
| | t = np.concatenate([-5 + 1.8 * rng.standard_cauchy(500), |
| | -4 + 0.8 * rng.standard_cauchy(2000), |
| | -1 + 0.3 * rng.standard_cauchy(500), |
| | 2 + 0.8 * rng.standard_cauchy(1000), |
| | 4 + 1.5 * rng.standard_cauchy(1000)]) |
| |
|
| | # truncate to a reasonable range |
| | t = t[(t > -15) & (t < 15)] |
| |
|
| | # draw histograms with two different bin widths |
| | fig, ax = plt.subplots(1, 2, figsize=(10, 4)) |
| |
|
| | fig.subplots_adjust(left=0.1, right=0.95, bottom=0.15) |
| | for i, bins in enumerate([ |
| | hist(t, bins=bins, ax=ax[i], histtype= |
| | alpha=0.2, density=True) |
| | ax[i].set_xlabel( |
| | ax[i].set_ylabel( |
| | ax[i].set_title( |
| | fontdict=dict(family= |
| |
|
| |
|
| | As we can see, both of these rules of thumb choose an intermediate number of |
| | bins which provide a good trade-off between data representation and noise |
| | suppression. |
| |
|
| | Bayesian Models |
| | =============== |
| |
|
| | Though rules-of-thumb like Scott |
| | fast and convenient, their strong assumptions about the data make them |
| | suboptimal for more complicated distributions. Other methods of bin selection |
| | use fitness functions computed on the actual data to choose an optimal binning. |
| | Astropy implements two of these examples: Knuth |
| | :func:`~astropy.stats.knuth_bin_width`) and Bayesian Blocks (implemented in |
| | :func:`~astropy.stats.bayesian_blocks`). |
| |
|
| | Knuth |
| | histogram |
| | flexible method which allows varying bin widths. Because both of these require |
| | the minimization of a cost function across the dataset, they are more |
| | computationally intensive than the rules-of-thumb mentioned above. Here are |
| | the results of these procedures for the above dataset: |
| |
|
| | .. plot:: |
| | :align: center |
| |
|
| | import warnings |
| | import numpy as np |
| | import matplotlib.pyplot as plt |
| | from astropy.visualization import hist |
| |
|
| | # generate some complicated data |
| | rng = np.random.RandomState(0) |
| | t = np.concatenate([-5 + 1.8 * rng.standard_cauchy(500), |
| | -4 + 0.8 * rng.standard_cauchy(2000), |
| | -1 + 0.3 * rng.standard_cauchy(500), |
| | 2 + 0.8 * rng.standard_cauchy(1000), |
| | 4 + 1.5 * rng.standard_cauchy(1000)]) |
| |
|
| | # truncate to a reasonable range |
| | t = t[(t > -15) & (t < 15)] |
| |
|
| | # draw histograms with two different bin widths |
| | fig, ax = plt.subplots(1, 2, figsize=(10, 4)) |
| |
|
| | fig.subplots_adjust(left=0.1, right=0.95, bottom=0.15) |
| | for i, bins in enumerate([ |
| | with warnings.catch_warnings(): |
| | warnings.simplefilter( |
| | hist(t, bins=bins, ax=ax[i], histtype= |
| | alpha=0.2, density=True) |
| | ax[i].set_xlabel( |
| | ax[i].set_ylabel( |
| | ax[i].set_title( |
| | fontdict=dict(family= |
| |
|
| |
|
| | Notice that both of these capture the shape of the distribution very |
| | accurately, and that the ``bins= |
| | in width depending on the local structure in the data. Compared to standard |
| | defaults, these Bayesian optimization methods provide a much more principled |
| | means of choosing histogram binning. |
| |
|