You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: hexbin cannot always accept np.max like functions as reduce_C_function #27103
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
importnumpyasnpimportmatplotlib.pyplotaspltx=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.
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).
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.
Activity
tacaswell commentedon Oct 16, 2023
As noted in #26113 (comment) there has been a bunch of discussion on this in #16865 and #21381
anntzer commentedon 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.