From 1344a9018f243281d09c29fdb788c9a966dc4714 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Mon, 23 Oct 2023 17:49:16 -0500 Subject: [PATCH 1/2] Restore default behavior of hexbin mincnt with C provided --- doc/api/next_api_changes/behavior/27179-KS.rst | 7 +++++++ .../prev_api_changes/api_changes_3.8.0/behaviour.rst | 6 ++++++ lib/matplotlib/axes/_axes.py | 11 ++++++++--- lib/matplotlib/tests/test_axes.py | 2 ++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 doc/api/next_api_changes/behavior/27179-KS.rst diff --git a/doc/api/next_api_changes/behavior/27179-KS.rst b/doc/api/next_api_changes/behavior/27179-KS.rst new file mode 100644 index 000000000000..fed4675a5f38 --- /dev/null +++ b/doc/api/next_api_changes/behavior/27179-KS.rst @@ -0,0 +1,7 @@ +Default behavior of ``hexbin`` with *C* provided requires at least 1 point +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The behavior changed in 3.8.0 to be inclusive of *mincnt*, however that resulted in +errors or warnings with some reduction functions, so now the default is to require at +least 1 point to call the reduction function. This effectively restores the default +behavior to match that of Matplotlib 3.7 and before. diff --git a/doc/api/prev_api_changes/api_changes_3.8.0/behaviour.rst b/doc/api/prev_api_changes/api_changes_3.8.0/behaviour.rst index 9a528bdbc251..3476a05394df 100644 --- a/doc/api/prev_api_changes/api_changes_3.8.0/behaviour.rst +++ b/doc/api/prev_api_changes/api_changes_3.8.0/behaviour.rst @@ -165,3 +165,9 @@ PostScript paper type adds option to use figure size The :rc:`ps.papertype` rcParam can now be set to ``'figure'``, which will use a paper size that corresponds exactly with the size of the figure that is being saved. + +``hexbin`` *mincnt* parameter made consistently inclusive +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Previously, *mincnt* was inclusive with no *C* provided but exclusive when *C* is provided. +It is now inclusive of *mincnt* in both cases. diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 0fcabac8c7c0..9199f1cafae3 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4873,8 +4873,8 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None, yscale : {'linear', 'log'}, default: 'linear' 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* + mincnt : int >= 0, default: *None* + If not *None*, only display cells with at least *mincnt* number of points in the cell. marginals : bool, default: *False* @@ -4941,6 +4941,11 @@ def reduce_C_function(C: array) -> float - `numpy.sum`: integral of the point values - `numpy.amax`: value taken from the largest point + By default will only reduce cells with at least 1 point because some + reduction functions (such as `numpy.amax`) will error/warn with empty + input. Changing *mincnt* will adjust the cutoff, and if set to 0 will + pass empty input to the reduction function. + data : indexable object, optional DATA_PARAMETER_PLACEHOLDER @@ -5038,7 +5043,7 @@ def reduce_C_function(C: array) -> float else: Cs_at_i2[i2[i]].append(C[i]) if mincnt is None: - mincnt = 0 + mincnt = 1 accum = np.array( [reduce_C_function(acc) if len(acc) >= mincnt else np.nan for Cs_at_i in [Cs_at_i1, Cs_at_i2] diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 564bf6a86b52..c82c79272d87 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -971,6 +971,8 @@ def test_hexbin_empty(): # From #23922: creating hexbin with log scaling from empty # dataset raises ValueError ax.hexbin([], [], bins='log') + # From #27103: np.max errors when handed empty data + ax.hexbin([], [], C=[], reduce_C_function=np.max) def test_hexbin_pickable(): From d2b2a069066eb6fe137685c33ba4139100e45e94 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Wed, 25 Oct 2023 15:08:11 -0500 Subject: [PATCH 2/2] Grammar in release note for hexbin mincnt Co-authored-by: Elliott Sales de Andrade --- doc/api/next_api_changes/behavior/27179-KS.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/next_api_changes/behavior/27179-KS.rst b/doc/api/next_api_changes/behavior/27179-KS.rst index fed4675a5f38..873cd622bbd4 100644 --- a/doc/api/next_api_changes/behavior/27179-KS.rst +++ b/doc/api/next_api_changes/behavior/27179-KS.rst @@ -1,7 +1,7 @@ Default behavior of ``hexbin`` with *C* provided requires at least 1 point ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The behavior changed in 3.8.0 to be inclusive of *mincnt*, however that resulted in +The behavior changed in 3.8.0 to be inclusive of *mincnt*. However that resulted in errors or warnings with some reduction functions, so now the default is to require at least 1 point to call the reduction function. This effectively restores the default behavior to match that of Matplotlib 3.7 and before.