{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Multiple histograms side by side\n\nThis example plots horizontal histograms of different samples along\na categorical x-axis. Additionally, the histograms are plotted to\nbe symmetrical about their x-position, thus making them very similar\nto violin plots.\n\nTo make this highly specialized plot, we can't use the standard ``hist``\nmethod. Instead, we use ``barh`` to draw the horizontal bars directly. The\nvertical positions and lengths of the bars are computed via the\n``np.histogram`` function. The histograms for all the samples are\ncomputed using the same range (min and max values) and number of bins,\nso that the bins for each sample are in the same vertical positions.\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)\nnumber_of_bins = 20\n\n# An example of three data sets to compare\nnumber_of_data_points = 387\nlabels = [\"A\", \"B\", \"C\"]\ndata_sets = [np.random.normal(0, 1, number_of_data_points),\n np.random.normal(6, 1, number_of_data_points),\n np.random.normal(-3, 1, number_of_data_points)]\n\n# Computed quantities to aid plotting\nhist_range = (np.min(data_sets), np.max(data_sets))\nbinned_data_sets = [\n np.histogram(d, range=hist_range, bins=number_of_bins)[0]\n for d in data_sets\n]\nbinned_maximums = np.max(binned_data_sets, axis=1)\nx_locations = np.arange(0, sum(binned_maximums), np.max(binned_maximums))\n\n# The bin_edges are the same for all of the histograms\nbin_edges = np.linspace(hist_range[0], hist_range[1], number_of_bins + 1)\nheights = np.diff(bin_edges)\ncenters = bin_edges[:-1] + heights / 2\n\n# Cycle through and plot each histogram\nfig, ax = plt.subplots()\nfor x_loc, binned_data in zip(x_locations, binned_data_sets):\n lefts = x_loc - 0.5 * binned_data\n ax.barh(centers, binned_data, height=heights, left=lefts)\n\nax.set_xticks(x_locations, labels)\n\nax.set_ylabel(\"Data values\")\nax.set_xlabel(\"Data sets\")\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n\n domain: statistics\n plot-type: barh\n plot-type: histogram\n styling: position\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.barh` / `matplotlib.pyplot.barh`\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 }