Skip to content

Commit f9bb47e

Browse files
authored
Merge pull request #20397 from timhoffm/doc-hexbin
Improve hexbin() documentation
2 parents cf1824f + aa2545f commit f9bb47e

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

examples/statistics/hexbin_demo.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
"""
2-
===========
3-
Hexbin Demo
4-
===========
2+
=====================
3+
Hexagonal binned plot
4+
=====================
55
6-
Plotting hexbins with Matplotlib.
7-
8-
Hexbin is an axes method or pyplot function that is essentially
9-
a pcolor of a 2D histogram with hexagonal cells. It can be
10-
much more informative than a scatter plot. In the first plot
11-
below, try substituting 'scatter' for 'hexbin'.
6+
`~.Axes.hexbin` is a 2D histogram plot, in which the bins are hexagons and
7+
the color represents the number of data points within each bin.
128
"""
139

1410
import numpy as np
@@ -17,29 +13,23 @@
1713
# Fixing random state for reproducibility
1814
np.random.seed(19680801)
1915

20-
n = 100000
16+
n = 100_000
2117
x = np.random.standard_normal(n)
2218
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
23-
xmin = x.min()
24-
xmax = x.max()
25-
ymin = y.min()
26-
ymax = y.max()
27-
28-
fig, axs = plt.subplots(ncols=2, sharey=True, figsize=(7, 4))
29-
fig.subplots_adjust(hspace=0.5, left=0.07, right=0.93)
30-
ax = axs[0]
31-
hb = ax.hexbin(x, y, gridsize=50, cmap='inferno')
32-
ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))
33-
ax.set_title("Hexagon binning")
34-
cb = fig.colorbar(hb, ax=ax)
35-
cb.set_label('counts')
36-
37-
ax = axs[1]
38-
hb = ax.hexbin(x, y, gridsize=50, bins='log', cmap='inferno')
39-
ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))
40-
ax.set_title("With a log color scale")
41-
cb = fig.colorbar(hb, ax=ax)
42-
cb.set_label('log10(N)')
19+
xlim = x.min(), x.max()
20+
ylim = y.min(), y.max()
21+
22+
fig, (ax0, ax1) = plt.subplots(ncols=2, sharey=True, figsize=(9, 4))
23+
24+
hb = ax0.hexbin(x, y, gridsize=50, cmap='inferno')
25+
ax0.set(xlim=xlim, ylim=ylim)
26+
ax0.set_title("Hexagon binning")
27+
cb = fig.colorbar(hb, ax=ax0, label='counts')
28+
29+
hb = ax1.hexbin(x, y, gridsize=50, bins='log', cmap='inferno')
30+
ax1.set(xlim=xlim, ylim=ylim)
31+
ax1.set_title("With a log color scale")
32+
cb = fig.colorbar(hb, ax=ax1, label='log10(N)')
4333

4434
plt.show()
4535

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4727,6 +4727,9 @@ def reduce_C_function(C: array) -> float
47274727
47284728
%(PolyCollection:kwdoc)s
47294729
4730+
See Also
4731+
--------
4732+
hist2d : 2D histogram rectangular bins
47304733
"""
47314734
self._process_unit_info([("x", x), ("y", y)], kwargs, convert=False)
47324735

@@ -6622,7 +6625,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
66226625
66236626
See Also
66246627
--------
6625-
hist2d : 2D histograms
6628+
hist2d : 2D histogram with rectangular bins
6629+
hexbin : 2D histogram with hexagonal bins
66266630
66276631
Notes
66286632
-----
@@ -7065,6 +7069,7 @@ def hist2d(self, x, y, bins=10, range=None, density=False, weights=None,
70657069
See Also
70667070
--------
70677071
hist : 1D histogram plotting
7072+
hexbin : 2D histogram with hexagonal bins
70687073
70697074
Notes
70707075
-----

0 commit comments

Comments
 (0)