Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Hexbin gridsize interpreted differently for x and y #21349

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
timhoffm opened this issue Oct 13, 2021 · 6 comments · Fixed by #24193
Closed

[Bug]: Hexbin gridsize interpreted differently for x and y #21349

timhoffm opened this issue Oct 13, 2021 · 6 comments · Fixed by #24193
Milestone

Comments

@timhoffm
Copy link
Member

timhoffm commented Oct 13, 2021

Bug summary

gridsize results in 2x the expected number of elements in y-direction

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)

fig, ax = plt.subplots(figsize=(6, 6))
h = ax.hexbin(x, y, gridsize=(10, 10), marginals=True, reduce_C_function=np.sum)

Actual outcome

grafik

Expected outcome

grafik

This image was taken with gridsize=(10, 5) but should be the result of gridsize=(10, 10).

@dstansby
Copy link
Member

Does anyone have opinions on deprecating and remove support for marginals altogether? I'd say it's an oddity within Matplotlib (we don't support it for pcolormesh, imshow etc.), and is high enough level to be out of scope (but probably the remit of say, seaborn)

@jklymak
Copy link
Member

jklymak commented Oct 14, 2021

@dstansby, I think the marginals are an orthogonal issue. The point is that the hexes are asymmetric. I think its easier to see if we set the aspect ratio of the data and plot to 1:

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
n = 100000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)

fig, ax = plt.subplots(figsize=(6, 6))
h = ax.hexbin(x, y, gridsize=(10, 10), marginals=True, reduce_C_function=np.sum)
ax.set_aspect(1)
plt.show()

hexbin

@anntzer anntzer mentioned this issue Oct 14, 2021
7 tasks
@anntzer
Copy link
Contributor

anntzer commented Oct 14, 2021

(I agree with @dstansby re: deprecating marginals, although it should be possible to write an example showcasing how to do them -- not only for hexbin, but also for other 2D maps.)

@dstansby
Copy link
Member

Sorry for the off topic comment - I'll open a new issue for that.

@tacaswell
Copy link
Member

We talked about this on the call this week https://hackmd.io/49a-u44CTja02xQRaG88JA?view#hexbin

The consensus action is :

  • update the docstring to be clear about what it is doing
  • possibly add a second mutually-exclusive keyword to go with gridsize that has the meaning expected

The docstring currently says:

       Alternatively, if a tuple (*nx*, *ny*), the number of hexagons
       in the *x*-direction and the *y*-direction.

Which is at best ambigious as if in these examples you count between hexs that are vertically aligned you (rather than zig-zag) you do get the expected number. To be clear, the stacking is ABAB, if you count the number of A you get the expected number.

We also need to inspect the code and make sure that this is not doing something about counting along the 60-degree diagonal and then filling in how many y rows we need. This would be consistent with what is done in the case of only passing 1 number (where the number of y-bins is xbin / sqrt(3) )

@timhoffm
Copy link
Member Author

I think clear documentation is sufficient. The common use case of regular hexagons is handled by the single-integer parameter. Asymmetric relations are less common and can be achieved using an appropriate scaling factor. We don't need additional mutually exclusive parameters for that.

@QuLogic QuLogic modified the milestones: v3.5.3, v3.5.4 Jul 21, 2022
@oscargus oscargus modified the milestones: v3.5.4, v3.6.1 Sep 25, 2022
@QuLogic QuLogic modified the milestones: v3.6.1, v3.6.2 Oct 6, 2022
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Oct 16, 2022
chahak13 pushed a commit to chahak13/matplotlib that referenced this issue Oct 18, 2022
melissawm pushed a commit to melissawm/matplotlib that referenced this issue Dec 19, 2022
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.

7 participants