{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# The histogram (hist) function with multiple data sets\n\nPlot histogram with multiple sample sets and demonstrate:\n\n* Use of legend with multiple sample sets\n* Stacked bars\n* Step curve with no fill\n* Data sets of different sample sizes\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\n.. redirect-from:: /gallery/lines_bars_and_markers/filled_step\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\nn_bins = 10\nx = np.random.randn(1000, 3)\n\nfig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2)\n\ncolors = ['red', 'tan', 'lime']\nax0.hist(x, n_bins, density=True, histtype='bar', color=colors, label=colors)\nax0.legend(prop={'size': 10})\nax0.set_title('bars with legend')\n\nax1.hist(x, n_bins, density=True, histtype='bar', stacked=True)\nax1.set_title('stacked bar')\n\nax2.hist(x, n_bins, histtype='step', stacked=True, fill=False)\nax2.set_title('stack step (unfilled)')\n\n# Make a multiple-histogram of data-sets with different length.\nx_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]\nax3.hist(x_multi, n_bins, histtype='bar')\nax3.set_title('different sample sizes')\n\nfig.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setting properties for each dataset\n\nYou can style the histograms individually by passing a list of values to the\nfollowing parameters:\n\n* edgecolor\n* facecolor\n* hatch\n* linewidth\n* linestyle\n\n\n### edgecolor\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nedgecolors = ['green', 'red', 'blue']\n\nax.hist(x, n_bins, fill=False, histtype=\"step\", stacked=True,\n edgecolor=edgecolors, label=edgecolors)\nax.legend()\nax.set_title('Stacked Steps with Edgecolors')\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### facecolor\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nfacecolors = ['green', 'red', 'blue']\n\nax.hist(x, n_bins, histtype=\"barstacked\", facecolor=facecolors, label=facecolors)\nax.legend()\nax.set_title(\"Bars with different Facecolors\")\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### hatch\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nhatches = [\".\", \"o\", \"x\"]\n\nax.hist(x, n_bins, histtype=\"barstacked\", hatch=hatches, label=hatches)\nax.legend()\nax.set_title(\"Hatches on Stacked Bars\")\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### linewidth\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nlinewidths = [1, 2, 3]\nedgecolors = [\"green\", \"red\", \"blue\"]\n\nax.hist(x, n_bins, fill=False, histtype=\"bar\", linewidth=linewidths,\n edgecolor=edgecolors, label=linewidths)\nax.legend()\nax.set_title(\"Bars with Linewidths\")\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### linestyle\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nlinestyles = ['-', ':', '--']\n\nax.hist(x, n_bins, fill=False, histtype='bar', linestyle=linestyles,\n edgecolor=edgecolors, label=linestyles)\nax.legend()\nax.set_title('Bars with Linestyles')\n\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.2" } }, "nbformat": 4, "nbformat_minor": 0 }