Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat: add dunder method for math operations on Axes Size divider #27938

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

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

paquiteau
Copy link
Contributor

PR summary

Closes #27930. This adds dunder methods (mul, and divide notably) to make interacting with axes_grid1.Size object better.

The original code for Size object interaction is not tested, so I don't know how to proceed to add some test for these changes. Overall, the direct usage of Size objects is quite niche, so I seek guidance as well for the documentation (maybe a new example ?)

PR checklist

Thanks in advance!

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Comment on lines 22 to 23
def __mul__(self, other):
return Fraction(self, other)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the implementation of Fraction this does not work. I'm sorry, this is not well documented, but the Fraction signature is def __init__(self, fraction : numbers.Real, ref_size : _Base), meaning a fraction of the given size. So 0.3 * mysize is 30% of mysize. If anything, you'd have to return Fraction(other, self) as well. But since this is basically a scaling factor, it may be better to only accept the factor on the left.

Comment on lines 25 to 26
def __div__(self, other):
return Fraction(self, 1/other)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above mysize / 2 is half of mysize. Here, it clearly does not make sense to divide something by a size. So this should not be added.

lib/mpl_toolkits/axes_grid1/axes_size.py Show resolved Hide resolved
lib/mpl_toolkits/axes_grid1/axes_size.py Outdated Show resolved Hide resolved
@paquiteau
Copy link
Contributor Author

Thanks for the early review!

Following your comments, I added some type checking for scaling operations.
A clear definition (and documentation) of the behavior could be specified. From what I understood from the code, Size can be scaled (e.g. multiply by a Real number) and added or substracted together (basically all operations defined for a linear vector space).

I do think that allowing multiplication on the right (or div) is nice to have (especially division): Typically to split an axis equally in n part:

divider.set_horizontal([Size.AxesX(ax) / n for _ in range(n)]

@tacaswell
Copy link
Member

Rather than TypeError we should be returning https://docs.python.org/3/library/constants.html#NotImplemented to play-nice with the fallback rules and letting the interpreter untimely raise the error.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have better documentation and tests on Size, but given that we don't have that in the current code, I'm not requiring it for this PR.

return Add(self, Fixed(other))

def __sub__(self, other):
if isinstance(other, _Base):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think __sub__ can just be defined (in all cases, including the exception-raising one) as self + (-other)?

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor nit.

lib/mpl_toolkits/axes_grid1/axes_size.py Outdated Show resolved Hide resolved
lib/mpl_toolkits/axes_grid1/axes_size.py Show resolved Hide resolved
lib/mpl_toolkits/axes_grid1/axes_size.py Outdated Show resolved Hide resolved
@anntzer
Copy link
Contributor

anntzer commented Sep 12, 2024

The pre-commit failure is real (typo; "subtraction", not "substraction"); also, can you squash the commits?

@anntzer anntzer merged commit 9365c97 into matplotlib:main Sep 13, 2024
39 of 40 checks passed
@anntzer
Copy link
Contributor

anntzer commented Sep 13, 2024

Thanks for the PR!

@tacaswell tacaswell added this to the v3.10.0 milestone Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for author
Development

Successfully merging this pull request may close these issues.

[ENH]: Make axes_grid1.Size more math friendly.
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.