Skip to content

[Bug]: Saving a figure with bbox_inches="tight" fails after inset_axes #26468

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
obriene1 opened this issue Aug 8, 2023 · 1 comment
Closed

Comments

@obriene1
Copy link

obriene1 commented Aug 8, 2023

Bug summary

After using inset_axes in mpl 3.7.2, savefig fails if bbox_inches is set to "tight". This does not fail in 3.7.1.

Code for reproduction

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

# Test data
x = [i for i in range(10)]
y = [i for i in range(10)]
x_sub = [i for i in range(5)]
y_sub = [i for i in range(5)]

# Initial figure
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)

# Insetted axis and plot
cax = inset_axes(ax, width="30%", height="30%", loc="lower right")
cax.plot(x_sub, y_sub)

# Saving -- error due to bbox_inches part
fig.savefig("test_out.png", format="png", bbox_inches="tight")

Actual outcome

AttributeError in line 16 (replaced paths to script and lib):

Traceback (most recent call last):
File "<path_to_script>/bug_test.py", line 16, in
fig.savefig("test_out.png", format="png", bbox_inches="tight")
File "<path_to_lib>/lib/python3.9/site-packages/matplotlib/figure.py", line 3378, in savefig
self.canvas.print_figure(fname, **kwargs)
File "<path_to_lib>/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 2353, in print_figure
restore_bbox = _tight_bbox.adjust_bbox(
File "<path_to_lib>/lib/python3.9/site-packages/matplotlib/_tight_bbox.py", line 28, in adjust_bbox
ax.apply_aspect(locator(ax, None))
File "<path_to_lib>/lib/python3.9/site-packages/mpl_toolkits/axes_grid1/inset_locator.py", line 73, in call
bbox = self.get_window_extent(renderer)
File "<path_to_lib>/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 399, in get_window_extent
renderer = self.figure._get_renderer()
AttributeError: 'NoneType' object has no attribute '_get_renderer'

Expected outcome

A straight line in the main plot with an insetted second line in the lower right corner.

test_out

Additional information

This worked in mpl 3.7.1 (see expected outcome).

This doesn't happen if the bbox_inches kwarg is removed.

Operating system

MacOS

Matplotlib Version

3.7.2

Matplotlib Backend

Installed osx event loop hook. MacOSX

Python version

3.9.6

Jupyter version

No response

Installation

pip

@rcomer
Copy link
Member

rcomer commented Aug 8, 2023

Thanks for the report @obriene1. It looks like this is the same as #26287 and was fixed by #26291 ready for the next release, which should be soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants