{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Demo of the histogram function's different ``histtype`` settings\n\n* Histogram with step curve that has a color fill.\n* Histogram with step curve with no fill.\n* Histogram with custom and unequal bin widths.\n* Two histograms with stacked bars.\n\nSelecting different bin counts and sizes can significantly affect the\nshape of a histogram. The Astropy docs have a great section on how to\nselect these parameters:\nhttp://docs.astropy.org/en/stable/visualization/histogram.html\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nnp.random.seed(19680801)\n\nmu_x = 200\nsigma_x = 25\nx = np.random.normal(mu_x, sigma_x, size=100)\n\nmu_w = 200\nsigma_w = 10\nw = np.random.normal(mu_w, sigma_w, size=100)\n\nfig, axs = plt.subplots(nrows=2, ncols=2)\n\naxs[0, 0].hist(x, 20, density=True, histtype='stepfilled', facecolor='g',\n alpha=0.75)\naxs[0, 0].set_title('stepfilled')\n\naxs[0, 1].hist(x, 20, density=True, histtype='step', facecolor='g',\n alpha=0.75)\naxs[0, 1].set_title('step')\n\naxs[1, 0].hist(x, density=True, histtype='barstacked', rwidth=0.8)\naxs[1, 0].hist(w, density=True, histtype='barstacked', rwidth=0.8)\naxs[1, 0].set_title('barstacked')\n\n# Create a histogram by providing the bin edges (unequally spaced).\nbins = [100, 150, 180, 195, 205, 220, 250, 300]\naxs[1, 1].hist(x, bins, density=True, histtype='bar', rwidth=0.8)\naxs[1, 1].set_title('bar, unequal bins')\n\nfig.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags:: plot-type: histogram, domain: statistics, purpose: reference\n\n.. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.hist` / `matplotlib.pyplot.hist`\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.5" } }, "nbformat": 4, "nbformat_minor": 0 }