Skip to content

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

Merged
merged 1 commit into from
Mar 20, 2023
Merged

Conversation

rcomer
Copy link
Member

@rcomer rcomer commented Mar 18, 2023

PR Summary

Fixes #22625. The problem was that _ColorbarAxesLocator was not used to set the aspect ratio before adjust_bbox stripped it off. A simpler but presumably less efficient fix might be to trigger a draw before calling adjust_bbox. We already do that if bbox_inches == "tight".

if layout_engine is not None or bbox_inches == "tight":
# we need to trigger a draw before printing to make sure
# CL works. "tight" also needs a draw to get the right
# locations:

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • [N/A] Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • [N/A] New plotting related features are documented with examples.

Release Notes

  • [N/A] New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • [N/A] API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • [N/A] Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

@rcomer rcomer added this to the v3.7.2 milestone Mar 18, 2023
@rcomer rcomer added the PR: bugfix Pull requests that fix identified bugs label Mar 18, 2023
@rcomer rcomer force-pushed the colorbar-bbox_inches branch from f0db686 to 9df8594 Compare March 18, 2023 18:36
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]]))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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]]))

Copy link
Member Author

@rcomer rcomer Mar 19, 2023

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.

Copy link
Contributor

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.
Copy link
Contributor

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?

Copy link
Member Author

@rcomer rcomer Mar 19, 2023

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?

Copy link
Contributor

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!

@rcomer rcomer force-pushed the colorbar-bbox_inches branch from 9df8594 to 50aa98a Compare March 19, 2023 19:22
@oscargus oscargus merged commit 8ef978d into matplotlib:main Mar 20, 2023
@lumberbot-app
Copy link

lumberbot-app bot commented Mar 20, 2023

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout v3.7.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 8ef978d3c3c9461cacb69bb222955c436ad65088
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #25499: FIX: use locators in adjust_bbox'
  1. Push to a named branch:
git push YOURFORK v3.7.x:auto-backport-of-pr-25499-on-v3.7.x
  1. Create a PR against branch v3.7.x, I would have named this PR:

"Backport PR #25499 on branch v3.7.x (FIX: use locators in adjust_bbox)"

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 Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@rcomer rcomer deleted the colorbar-bbox_inches branch March 20, 2023 21:05
rcomer pushed a commit to rcomer/matplotlib that referenced this pull request Mar 21, 2023
Conflict was caused by the new test being right above the test from

FIX: use locators in adjust_bbox
(cherry picked from commit 8ef978d)
rcomer pushed a commit to rcomer/matplotlib that referenced this pull request Mar 21, 2023
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)
ksunden added a commit that referenced this pull request Mar 21, 2023
Backport PR #25499: FIX: use locators in adjust_bbox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: bugfix Pull requests that fix identified bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Setting bbox_inches to a Bbox in fig.savefig resizes colorbar
3 participants