Skip to content

[Bug]: hexbin cannot always accept np.max like functions as reduce_C_function #27103

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

Closed
materia9 opened this issue Oct 16, 2023 · 2 comments · Fixed by #27179
Closed

[Bug]: hexbin cannot always accept np.max like functions as reduce_C_function #27103

materia9 opened this issue Oct 16, 2023 · 2 comments · Fixed by #27179

Comments

@materia9
Copy link

materia9 commented Oct 16, 2023

Bug summary

The default reduce_C_function (np.mean) returns np.nan for size 0 sequence and hexbin does not cause Error. However, the default mincnt (=0) and reduce_C_function=np.max can cause Error for some gridsize & extent settings due to zero-sized sequence.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
x=np.array([0. , 1. , 2. , 0.5, 1.5, 2.5, 0. , 1. , 2. ])
y=np.array([0.        , 0.        , 0.        , 0.8660254 , 0.8660254 ,
       0.8660254 , 1.73205081, 1.73205081, 1.73205081])
extent=(0,2.5,0,np.sqrt(3))
C=np.arange(x.size)
gridsize=(3,1)
plt.hexbin(x,y,C, extent=extent, reduce_C_function=np.max, gridsize=gridsize)
# ValueError: zero-size array to reduction operation maximum which has no identity

Actual outcome

ValueError will be raised by numpy max(), which cannot accept zero sized sequence.

Expected outcome

No error is raised.

Additional information

This bug/new specification has been introduced by

fcda8c8

Apparently, the matplotlib versions>=3.8.0 would have the behavior but versions<3.8.0 do not.

The default reduce_C_function (np.mean) returns np.nan for size 0 sequence and it does not cause Error. However, the default mincnt (None and mincnt=0 is assigned for None) and reduce_C_function=np.max can cause Error due to zero-sized sequence. The old versions use 'len(acc) > mincnt' comparison and it kicks size 0 sequence by default. However, the new version uses 'len(acc) >= mincnt' comparison but the default mincnt is still 0. Hence, 0 sized input is not kicked and numpy max() like functions raise Error. It can be fixed by mincnt=0.5 (1 is not compatible with the old matplotlib versions).

plt.hexbin(x,y,C, extent=extent, reduce_C_function=np.max, gridsize=gridsize, mincnt=0.5)

If this is a desired (planed) behavior, it would be nice if the documents (release notes) describe the behavior explicitly. At least my program won't work before fixing the problem, and I have to read the codes and their commit history to find the root cause.

Best regards

Operating system

Windows and Ubuntu

Matplotlib Version

3.8.0

Matplotlib Backend

Qt5Agg or Agg

Python version

3.11.4

Jupyter version

NA

Installation

pip

@tacaswell tacaswell added this to the v3.8.1 milestone Oct 16, 2023
@tacaswell
Copy link
Member

As noted in #26113 (comment) there has been a bunch of discussion on this in #16865 and #21381

@anntzer
Copy link
Contributor

anntzer commented Oct 16, 2023

I still believe the fix implemented in #26113 was wrong (as detailed the comment above), or at least needs some additional guards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants