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

Skip to content

Commit b5fd5d2

Browse files
committed
Cleanup hexbin.
Use `&` instead of `*` for boolean arrays; vectorize accumulation instead of iterating over individual entries by hand; misc. style cleanups.
1 parent 34970ed commit b5fd5d2

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4675,26 +4675,14 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
46754675
if C is None:
46764676
lattice1 = np.zeros((nx1, ny1))
46774677
lattice2 = np.zeros((nx2, ny2))
4678-
4679-
cond1 = (0 <= ix1) * (ix1 < nx1) * (0 <= iy1) * (iy1 < ny1)
4680-
cond2 = (0 <= ix2) * (ix2 < nx2) * (0 <= iy2) * (iy2 < ny2)
4681-
4682-
cond1 *= bdist
4683-
cond2 *= np.logical_not(bdist)
4684-
ix1, iy1 = ix1[cond1], iy1[cond1]
4685-
ix2, iy2 = ix2[cond2], iy2[cond2]
4686-
4687-
for ix, iy in zip(ix1, iy1):
4688-
lattice1[ix, iy] += 1
4689-
for ix, iy in zip(ix2, iy2):
4690-
lattice2[ix, iy] += 1
4691-
4692-
# threshold
4678+
c1 = (0 <= ix1) & (ix1 < nx1) & (0 <= iy1) & (iy1 < ny1) & bdist
4679+
c2 = (0 <= ix2) & (ix2 < nx2) & (0 <= iy2) & (iy2 < ny2) & ~bdist
4680+
np.add.at(lattice1, (ix1[c1], iy1[c1]), 1)
4681+
np.add.at(lattice2, (ix2[c2], iy2[c2]), 1)
46934682
if mincnt is not None:
46944683
lattice1[lattice1 < mincnt] = np.nan
46954684
lattice2[lattice2 < mincnt] = np.nan
4696-
accum = np.hstack((lattice1.ravel(),
4697-
lattice2.ravel()))
4685+
accum = np.concatenate([lattice1.ravel(), lattice2.ravel()])
46984686
good_idxs = ~np.isnan(accum)
46994687

47004688
else:
@@ -4751,9 +4739,8 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
47514739
offsets = offsets[good_idxs, :]
47524740
accum = accum[good_idxs]
47534741

4754-
polygon = np.zeros((6, 2), float)
4755-
polygon[:, 0] = sx * np.array([0.5, 0.5, 0.0, -0.5, -0.5, 0.0])
4756-
polygon[:, 1] = sy * np.array([-0.5, 0.5, 1.0, 0.5, -0.5, -1.0]) / 3.0
4742+
polygon = [sx, sy / 3] * np.array(
4743+
[[.5, -.5], [.5, .5], [0., 1.], [-.5, .5], [-.5, -.5], [0., -1.]])
47574744

47584745
if linewidths is None:
47594746
linewidths = [1.0]

0 commit comments

Comments
 (0)