-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
FIX: use locators in adjust_bbox #25499
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
Conversation
f0db686
to
9df8594
Compare
lib/matplotlib/tests/test_figure.py
Outdated
fig, ax = plt.subplots() | ||
pc = ax.pcolormesh(np.random.randn(2, 2)) | ||
cbar = fig.colorbar(pc, aspect=40) | ||
fig.savefig(tmp_path / 'foo.png', bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fig.savefig(tmp_path / 'foo.png', bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]])) | |
fig.savefig(io.BytesIO(), bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I didn't know you could do this but I see it is the convention through this module. So I took the liberty of also updating the test I added at #24971.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is likely faster to stay in memory when saving if possible rather than thrashing the hard drive, which I think is why this would be preferred.
cbar = fig.colorbar(pc, aspect=40) | ||
fig.savefig(tmp_path / 'foo.png', bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]])) | ||
|
||
# Check that an aspect ratio has been applied. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you able to assert against the aspect ratio too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't figure out how to check the aspect ratio directly. get_aspect()
returns "auto" before and after save. get_box_aspect()
returns 40 before and after save. Is there something else I've missed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know either, I just figured since it was the aspect that was wrong that is better to test against, so figured I would ask if you tried :) but colorbars are never straightforward and there is still an open issue with the get/set aspect on them. #22103
Although here it could also be that the aspect is only changed in the setattr context manager while saving the figure, so you don't get it except while in that context manager?
This still looks good to me!
9df8594
to
50aa98a
Compare
Owee, I'm MrMeeseeks, Look at me. There seem to be a conflict, please backport manually. Here are approximate instructions:
And apply the correct labels and milestones. Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon! Remember to remove the If these instructions are inaccurate, feel free to suggest an improvement. |
Conflict was caused by the new test being right above the test from FIX: use locators in adjust_bbox (cherry picked from commit 8ef978d)
Conflict was caused by the new test being right above the test from matplotlib#24816, which is not in the v3.7.x branch. FIX: use locators in adjust_bbox (cherry picked from commit 8ef978d)
Backport PR #25499: FIX: use locators in adjust_bbox
PR Summary
Fixes #22625. The problem was that
_ColorbarAxesLocator
was not used to set the aspect ratio beforeadjust_bbox
stripped it off. A simpler but presumably less efficient fix might be to trigger a draw before callingadjust_bbox
. We already do that ifbbox_inches == "tight"
.matplotlib/lib/matplotlib/backend_bases.py
Lines 2358 to 2361 in 78bf53c
PR Checklist
Documentation and Tests
pytest
passes)Release Notes
.. versionadded::
directive in the docstring and documented indoc/users/next_whats_new/
.. versionchanged::
directive in the docstring and documented indoc/api/next_api_changes/
next_whats_new/README.rst
ornext_api_changes/README.rst