Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b179b29

Browse files
committed
FIX: hexbin to return grid info
1 parent a90d5e9 commit b179b29

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
from matplotlib.axes._secondary_axes import SecondaryAxis
3838
from matplotlib.container import BarContainer, ErrorbarContainer, StemContainer
3939
from matplotlib.hexbin import hexbin
40-
4140
_log = logging.getLogger(__name__)
4241

4342

@@ -5137,12 +5136,22 @@ def reduce_C_function(C: array) -> float
51375136
self._process_unit_info([("x", x), ("y", y)], kwargs, convert=False)
51385137

51395138
x, y, C = cbook.delete_masked_points(x, y, C)
5140-
5141-
offsets, accum, (sx, sy) = hexbin(x, y, C, gridsize,
5142-
xscale, yscale, extent,
5143-
reduce_C_function, mincnt)
5144-
5145-
polygon = [sx, sy] * np.array(
5139+
5140+
# Count the number of data in each hexagon
5141+
x = np.asarray(x, float)
5142+
y = np.asarray(y, float)
5143+
5144+
(*offsets, accum), (xmin, xmax), (ymin, ymax), (nx, ny) = hexbin(
5145+
x, y, C, gridsize, xscale, yscale, extent, reduce_C_function, mincnt
5146+
)
5147+
offsets = np.transpose(offsets)
5148+
sx = (xmax - xmin) / nx
5149+
sy = (ymax - ymin) / ny
5150+
5151+
if C is None:
5152+
C = np.ones(len(x))
5153+
5154+
polygon = [sx, sy / 3] * np.array(
51465155
[[.5, -.5], [.5, .5], [0., 1.], [-.5, .5], [-.5, -.5], [0., -1.]])
51475156

51485157
if linewidths is None:
@@ -8658,4 +8667,4 @@ def _get_aspect_ratio(self):
86588667
figure_size = self.get_figure().get_size_inches()
86598668
ll, ur = self.get_position() * figure_size
86608669
width, height = ur - ll
8661-
return height / (width * self.get_data_ratio())
8670+
return height / (width * self.get_data_ratio())

lib/matplotlib/hexbin.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ def hexbin(x, y, C=None, gridsize=100,
1919
nx = gridsize
2020
ny = int(nx / math.sqrt(3))
2121

22-
# Count the number of data in each hexagon
23-
x = np.asarray(x, float)
24-
y = np.asarray(y, float)
25-
2622
# Will be log()'d if necessary, and then rescaled.
2723
tx = x
2824
ty = y
@@ -83,7 +79,7 @@ def hexbin(x, y, C=None, gridsize=100,
8379
accum = np.concatenate([counts1, counts2]).astype(float)
8480
if mincnt is not None:
8581
accum[accum < mincnt] = np.nan
86-
C = np.ones(len(x))
82+
8783
else:
8884
# store the C values in a list per hexagon index
8985
Cs_at_i1 = [[] for _ in range(1 + nx1 * ny1)]
@@ -96,9 +92,9 @@ def hexbin(x, y, C=None, gridsize=100,
9692
if mincnt is None:
9793
mincnt = 0
9894
accum = np.array(
99-
[reduce_C_function(acc) if len(acc) > mincnt else np.nan
100-
for Cs_at_i in [Cs_at_i1, Cs_at_i2]
101-
for acc in Cs_at_i[1:]], # [1:] drops out-of-range points.
95+
[reduce_C_function(acc) if len(acc) >= mincnt else np.nan
96+
for Cs_at_i in [Cs_at_i1, Cs_at_i2]
97+
for acc in Cs_at_i[1:]], # [1:] drops out-of-range points.
10298
float)
10399

104100
good_idxs = ~np.isnan(accum)
@@ -116,4 +112,4 @@ def hexbin(x, y, C=None, gridsize=100,
116112
offsets = offsets[good_idxs, :]
117113
accum = accum[good_idxs]
118114

119-
return (*offsets.T, accum), (sx, sy / 3)
115+
return (*offsets.T, accum), (xmin, xmax), (ymin, ymax), (nx, ny)

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def get_gaussian_hexs(mu=(0, 0),
5959
seed=123):
6060
np.random.seed(seed)
6161
rv = multivariate_normal(mu, np.array(sigma))
62-
xyz, dxy = hexbin(*rv.rvs(n).T, gridsize=res)
63-
return *xyz, np.array(dxy) * 0.9
62+
xyz, (xmin, xmax), (ymin, ymax), (nx, ny) = hexbin(*rv.rvs(n).T, gridsize=res)
63+
dxy = np.array([(xmax - xmin) / nx, (ymax - ymin) / ny]) * 0.9
64+
return *xyz, dxy
6465

6566

6667
bar3d_data_generators = {

0 commit comments

Comments
 (0)