From bc6bccc0369c185f70a801eebefc6c665abd9348 Mon Sep 17 00:00:00 2001 From: Rebecca Perry Date: Mon, 18 Oct 2021 22:23:00 -0400 Subject: [PATCH 1/3] BF - adding fix for hexbin mincnt --- lib/matplotlib/axes/_axes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 91badadf088e..ed7a6df241e3 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4696,14 +4696,14 @@ def reduce_C_function(C: array) -> float for i in range(nx1): for j in range(ny1): vals = lattice1[i, j] - if len(vals) > mincnt: + if len(vals) >= mincnt: lattice1[i, j] = reduce_C_function(vals) else: lattice1[i, j] = np.nan for i in range(nx2): for j in range(ny2): vals = lattice2[i, j] - if len(vals) > mincnt: + if len(vals) >= mincnt: lattice2[i, j] = reduce_C_function(vals) else: lattice2[i, j] = np.nan From bd24a0a08b6a50b657c1ef9b8a480455ccb883fe Mon Sep 17 00:00:00 2001 From: Rebecca Perry Date: Tue, 19 Oct 2021 07:13:26 -0400 Subject: [PATCH 2/3] doc - clarifying that mincount is inclusive --- lib/matplotlib/axes/_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index ed7a6df241e3..7f1d33288443 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4507,7 +4507,7 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None, Use a linear or log10 scale on the vertical axis. mincnt : int > 0, default: *None* - If not *None*, only display cells with more than *mincnt* + If not *None*, only display cells with at least *mincnt* number of points in the cell. marginals : bool, default: *False* From efa7e600cbb4900eb1103c371208c2e51c3ac6fa Mon Sep 17 00:00:00 2001 From: Rebecca Perry Date: Fri, 22 Oct 2021 11:09:24 -0400 Subject: [PATCH 3/3] adding hexbin C weights/mincnt test --- lib/matplotlib/tests/test_axes.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 09d0dac49903..be99283bcf24 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -793,6 +793,27 @@ def test_hexbin_log_clim(): assert h.get_clim() == (2, 100) +def test_hexbin_mincnt_withC(): + # Issue #20877: Tests mincnt working the same with/without weights C + np.random.seed(19680801) + n = 20000 + + x = np.random.normal(loc=0, scale=2, size=n) + y = np.random.randint(0, 100, n) + + fig, axes = plt.subplots(2, 1) + + ax = axes[0] + res1 = ax.hexbin(x, y, mincnt=1, extent=[-8, 8, 0, 100], gridsize=150) + + ax = axes[1] + res2 = ax.hexbin(x, y, mincnt=1, extent=[-8, 8, 0, 100], gridsize=150, + C=5*np.ones(len(x)), reduce_C_function=np.sum) + + assert len(res1.get_array()) == len(res2.get_array()) + assert 5*sum(res1.get_array()) == sum(res2.get_array()) + + def test_inverted_limits(): # Test gh:1553 # Calling invert_xaxis prior to plotting should not disable autoscaling