Skip to content

Commit 0486d36

Browse files
authored
Merge pull request #27201 from meeseeksmachine/auto-backport-of-pr-27179-on-v3.8.x
Backport PR #27179 on branch v3.8.x (Restore default behavior of hexbin mincnt with C provided)
2 parents 3566405 + b3bf965 commit 0486d36

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Default behavior of ``hexbin`` with *C* provided requires at least 1 point
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The behavior changed in 3.8.0 to be inclusive of *mincnt*. However that resulted in
5+
errors or warnings with some reduction functions, so now the default is to require at
6+
least 1 point to call the reduction function. This effectively restores the default
7+
behavior to match that of Matplotlib 3.7 and before.

doc/api/prev_api_changes/api_changes_3.8.0/behaviour.rst

+6
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,9 @@ PostScript paper type adds option to use figure size
165165
The :rc:`ps.papertype` rcParam can now be set to ``'figure'``, which will use
166166
a paper size that corresponds exactly with the size of the figure that is being
167167
saved.
168+
169+
``hexbin`` *mincnt* parameter made consistently inclusive
170+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171+
172+
Previously, *mincnt* was inclusive with no *C* provided but exclusive when *C* is provided.
173+
It is now inclusive of *mincnt* in both cases.

lib/matplotlib/axes/_axes.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -4858,8 +4858,8 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
48584858
yscale : {'linear', 'log'}, default: 'linear'
48594859
Use a linear or log10 scale on the vertical axis.
48604860
4861-
mincnt : int > 0, default: *None*
4862-
If not *None*, only display cells with more than *mincnt*
4861+
mincnt : int >= 0, default: *None*
4862+
If not *None*, only display cells with at least *mincnt*
48634863
number of points in the cell.
48644864
48654865
marginals : bool, default: *False*
@@ -4926,6 +4926,11 @@ def reduce_C_function(C: array) -> float
49264926
- `numpy.sum`: integral of the point values
49274927
- `numpy.amax`: value taken from the largest point
49284928
4929+
By default will only reduce cells with at least 1 point because some
4930+
reduction functions (such as `numpy.amax`) will error/warn with empty
4931+
input. Changing *mincnt* will adjust the cutoff, and if set to 0 will
4932+
pass empty input to the reduction function.
4933+
49294934
data : indexable object, optional
49304935
DATA_PARAMETER_PLACEHOLDER
49314936
@@ -5023,7 +5028,7 @@ def reduce_C_function(C: array) -> float
50235028
else:
50245029
Cs_at_i2[i2[i]].append(C[i])
50255030
if mincnt is None:
5026-
mincnt = 0
5031+
mincnt = 1
50275032
accum = np.array(
50285033
[reduce_C_function(acc) if len(acc) >= mincnt else np.nan
50295034
for Cs_at_i in [Cs_at_i1, Cs_at_i2]

lib/matplotlib/tests/test_axes.py

+2
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,8 @@ def test_hexbin_empty():
965965
# From #23922: creating hexbin with log scaling from empty
966966
# dataset raises ValueError
967967
ax.hexbin([], [], bins='log')
968+
# From #27103: np.max errors when handed empty data
969+
ax.hexbin([], [], C=[], reduce_C_function=np.max)
968970

969971

970972
def test_hexbin_pickable():

0 commit comments

Comments
 (0)