Skip to content

Backport PR #27179 on branch v3.8.x (Restore default behavior of hexbin mincnt with C provided) #27201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/behavior/27179-KS.rst
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions doc/api/prev_api_changes/api_changes_3.8.0/behaviour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
11 changes: 8 additions & 3 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4858,8 +4858,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*
Expand Down Expand Up @@ -4926,6 +4926,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

Expand Down Expand Up @@ -5023,7 +5028,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]
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,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():
Expand Down