Skip to content

Improvements and bugfixes for hexbin marginals #18875

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

Conversation

MihaiBabiac
Copy link

@MihaiBabiac MihaiBabiac commented Nov 2, 2020

PR Summary

The marginals functionality for hexbin had a couple of hidden bugs and issues:

  1. Providing hexbin with a tuple for the gridsize resulted in an error.
  2. It was possible to give the extents in the wrong order. This did not raise an error, but broke the marginals. (Error on bad input to hexbin extents #27607)
  3. The marginals were taking into account points outside of the specified limits.
  4. The bins for the marginals were not aligned in any way to the 2D hexagonal bins.
  5. The marginals tried using reduce_C_function even when C was not provided. With the default reduce_C_function, this meant applying np.mean to a set of ones, resulting in only binary values in the marginal.

This PR fixes these issues. It makes marginals work with tuple gridsize, raises a ValueError when the extents are in the wrong order, ignores points outside the extents, aligns the bins of the marginals to the hexagonal bins and uses summation when C is not provided.

In addition I changed the formatting of the code for the y marginal to match that of the x marginal so that it's easier to compare (and diff) them. You could consider somehow merging the two with a function, since most of the code is actually quite repetitive.

What would really be useful would be some unit tests for the hexbin marginals, but maybe someone else can help with that?

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@MihaiBabiac
Copy link
Author

This is the code I used for testing

import numpy as np
from matplotlib import pyplot as plt

np.random.seed(0)
x = np.random.normal(1, 1.5, 1000)
y = np.random.normal(1, 1.5, 1000)

plt.figure()
plt.hexbin(x, y,
           gridsize=7,
           marginals=True,
           reduce_C_function=np.sum,
           extent=(-2, 5, -3, 5))  # extents are here in correct order
plt.grid()
plt.show()

plt.figure()
plt.hexbin(x, y,
           gridsize=10,
           marginals=True,
           reduce_C_function=np.sum,
           extent=(-2, 5, 5, -3))  # extents are here in wrong order
plt.grid()
plt.show()

@QuLogic
Copy link
Member

QuLogic commented Nov 3, 2020

Can you add tests for the new stuff?

@MihaiBabiac
Copy link
Author

I can try. It's my first time contributing here, so I'm not sure what the tests should check. Can you give me some pointers?

@QuLogic
Copy link
Member

QuLogic commented Nov 16, 2020

Tests for hexbin would likely go in test_axes.py; see the developer guide on testing for more info.

@jklymak jklymak marked this pull request as draft April 23, 2021 15:34
@jklymak
Copy link
Member

jklymak commented Apr 23, 2021

ping for tests

@MihaiBabiac
Copy link
Author

I'm sorry for leaving it in this state, but probably in a few weeks is the earliest I'll have time to learn how the test system works and add the tests. It might make more sense for someone else to take it over.

@jklymak
Copy link
Member

jklymak commented Apr 23, 2021

I'll mark as orphaned if someone else wants to take it up. On the other hand, you are more than welcome to readopt your own baby.

The tests are in lib/matplotlib/tests and there are probably some hex bin tests you could grep for already...

@dstansby
Copy link
Member

dstansby commented Jan 6, 2024

I'm going to try and revive this and split into multiple PRs. I'll add links in the original description at the top where the individual issues are helpfully listed.

@dstansby
Copy link
Member

dstansby commented Jan 6, 2024

Apart from the new error in the PR I opened, I think the rest of the issues were fixed (possibly by #21039?) at some point between this PR being opened and now, so I'll close this PR. Feel free to open new issues about the hexbin marginals if I've got that wrong!

@dstansby dstansby closed this Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants