FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

FIX: use locators in adjust_bbox by rcomer · Pull Request #25499 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 4 additions & 1 deletion lib/matplotlib/_tight_bbox.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def adjust_bbox(fig, bbox_inches, fixed_dpi=None):
locator_list = []
sentinel = object()
for ax in fig.axes:
locator_list.append(ax.get_axes_locator())
locator = ax.get_axes_locator()
if locator is not None:
ax.apply_aspect(locator(ax, None))
locator_list.append(locator)
current_pos = ax.get_position(original=False).frozen()
ax.set_axes_locator(lambda a, r, _pos=current_pos: _pos)
# override the method that enforces the aspect ratio on the Axes
Expand Down
15 changes: 13 additions & 2 deletions lib/matplotlib/tests/test_figure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,24 @@ def test_savefig_pixel_ratio(backend):
assert ratio1 == ratio2


def test_savefig_preserve_layout_engine(tmp_path):
def test_savefig_preserve_layout_engine():
fig = plt.figure(layout='compressed')
fig.savefig(tmp_path / 'foo.png', bbox_inches='tight')
fig.savefig(io.BytesIO(), bbox_inches='tight')

assert fig.get_layout_engine()._compress


def test_savefig_locate_colorbar():
fig, ax = plt.subplots()
pc = ax.pcolormesh(np.random.randn(2, 2))
cbar = fig.colorbar(pc, aspect=40)
fig.savefig(io.BytesIO(), bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]]))

# Check that an aspect ratio has been applied.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Are you able to assert against the aspect ratio too?

rcomer Mar 19, 2023 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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!

assert (cbar.ax.get_position(original=True).bounds !=
cbar.ax.get_position(original=False).bounds)


@mpl.rc_context({"savefig.transparent": True})
@check_figures_equal(extensions=["png"])
def test_savefig_transparent(fig_test, fig_ref):
Expand Down

Back | FazBrowse Home | New Git URL