Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve hexbin() documentation #20397

Merged
merged 1 commit into from Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 20 additions & 30 deletions examples/statistics/hexbin_demo.py
@@ -1,14 +1,10 @@
"""
===========
Hexbin Demo
===========
=====================
Hexagonal binned plot
=====================

Plotting hexbins with Matplotlib.

Hexbin is an axes method or pyplot function that is essentially
a pcolor of a 2D histogram with hexagonal cells. It can be
much more informative than a scatter plot. In the first plot
below, try substituting 'scatter' for 'hexbin'.
`~.Axes.hexbin` is a 2D histogram plot, in which the bins are hexagons and
the color represents the number of data points within each bin.
"""

import numpy as np
Expand All @@ -17,29 +13,23 @@
# Fixing random state for reproducibility
np.random.seed(19680801)

n = 100000
n = 100_000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
xmin = x.min()
xmax = x.max()
ymin = y.min()
ymax = y.max()

fig, axs = plt.subplots(ncols=2, sharey=True, figsize=(7, 4))
fig.subplots_adjust(hspace=0.5, left=0.07, right=0.93)
ax = axs[0]
hb = ax.hexbin(x, y, gridsize=50, cmap='inferno')
ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))
ax.set_title("Hexagon binning")
cb = fig.colorbar(hb, ax=ax)
cb.set_label('counts')

ax = axs[1]
hb = ax.hexbin(x, y, gridsize=50, bins='log', cmap='inferno')
ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))
ax.set_title("With a log color scale")
cb = fig.colorbar(hb, ax=ax)
cb.set_label('log10(N)')
xlim = x.min(), x.max()
ylim = y.min(), y.max()

fig, (ax0, ax1) = plt.subplots(ncols=2, sharey=True, figsize=(9, 4))

hb = ax0.hexbin(x, y, gridsize=50, cmap='inferno')
ax0.set(xlim=xlim, ylim=ylim)
ax0.set_title("Hexagon binning")
cb = fig.colorbar(hb, ax=ax0, label='counts')

hb = ax1.hexbin(x, y, gridsize=50, bins='log', cmap='inferno')
ax1.set(xlim=xlim, ylim=ylim)
ax1.set_title("With a log color scale")
cb = fig.colorbar(hb, ax=ax1, label='log10(N)')

plt.show()

Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/axes/_axes.py
Expand Up @@ -4755,6 +4755,9 @@ def reduce_C_function(C: array) -> float

%(PolyCollection:kwdoc)s

See Also
--------
hist2d : 2D histogram rectangular bins
"""
self._process_unit_info([("x", x), ("y", y)], kwargs, convert=False)

Expand Down Expand Up @@ -6650,7 +6653,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,

See Also
--------
hist2d : 2D histograms
hist2d : 2D histogram with rectangular bins
hexbin : 2D histogram with hexagonal bins

Notes
-----
Expand Down Expand Up @@ -7093,6 +7097,7 @@ def hist2d(self, x, y, bins=10, range=None, density=False, weights=None,
See Also
--------
hist : 1D histogram plotting
hexbin : 2D histogram with hexagonal bins

Notes
-----
Expand Down