{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Hexagonal binned plot\n\n`~.Axes.hexbin` is a 2D histogram plot, in which the bins are hexagons and\nthe color represents the number of data points within each bin.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\nn = 100_000\nx = np.random.standard_normal(n)\ny = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)\nxlim = x.min(), x.max()\nylim = y.min(), y.max()\n\nfig, (ax0, ax1) = plt.subplots(ncols=2, sharey=True, figsize=(9, 4))\n\nhb = ax0.hexbin(x, y, gridsize=50, cmap='inferno')\nax0.set(xlim=xlim, ylim=ylim)\nax0.set_title(\"Hexagon binning\")\ncb = fig.colorbar(hb, ax=ax0, label='counts')\n\nhb = ax1.hexbin(x, y, gridsize=50, bins='log', cmap='inferno')\nax1.set(xlim=xlim, ylim=ylim)\nax1.set_title(\"With a log color scale\")\ncb = fig.colorbar(hb, ax=ax1, label='counts')\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags:: plot-type: histogram, plot-type: hexbin, domain: statistics\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.hexbin` / `matplotlib.pyplot.hexbin`\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 }