|
1 | 1 | """
|
2 |
| -=========== |
3 |
| -Hexbin Demo |
4 |
| -=========== |
| 2 | +===================== |
| 3 | +Hexagonal binned plot |
| 4 | +===================== |
5 | 5 |
|
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. |
12 | 8 | """
|
13 | 9 |
|
14 | 10 | import numpy as np
|
|
17 | 13 | # Fixing random state for reproducibility
|
18 | 14 | np.random.seed(19680801)
|
19 | 15 |
|
20 |
| -n = 100000 |
| 16 | +n = 100_000 |
21 | 17 | x = np.random.standard_normal(n)
|
22 | 18 | 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)') |
43 | 33 |
|
44 | 34 | plt.show()
|
45 | 35 |
|
|
0 commit comments