Skip to content

[FIX]: Make inset axes transparent on savefig(..., transparent=True) #24816

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
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3316,12 +3316,37 @@ def savefig(self, fname, *, transparent=None, **kwargs):

with ExitStack() as stack:
if transparent:
def _recursively_make_subfig_transparent(exit_stack, subfig):
exit_stack.enter_context(
subfig.patch._cm_set(
facecolor="none", edgecolor="none"))
for ax in subfig.axes:
exit_stack.enter_context(
ax.patch._cm_set(
facecolor="none", edgecolor="none"))
for sub_subfig in subfig.subfigs:
_recursively_make_subfig_transparent(
exit_stack, sub_subfig)

def _recursively_make_axes_transparent(exit_stack, ax):
exit_stack.enter_context(
ax.patch._cm_set(facecolor="none", edgecolor="none"))
for child_ax in ax.child_axes:
exit_stack.enter_context(
child_ax.patch._cm_set(
facecolor="none", edgecolor="none"))
for child_childax in ax.child_axes:
_recursively_make_axes_transparent(
exit_stack, child_childax)

kwargs.setdefault('facecolor', 'none')
kwargs.setdefault('edgecolor', 'none')
# set subfigure to appear transparent in printed image
for subfig in self.subfigs:
_recursively_make_subfig_transparent(stack, subfig)
# set axes to be transparent
for ax in self.axes:
stack.enter_context(
ax.patch._cm_set(facecolor='none', edgecolor='none'))

_recursively_make_axes_transparent(stack, ax)
self.canvas.print_figure(fname, **kwargs)

def ginput(self, n=1, timeout=30, show_clicks=True,
Expand Down
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,29 @@ def test_savefig_preserve_layout_engine(tmp_path):
assert fig.get_layout_engine()._compress


@mpl.rc_context({"savefig.transparent": True})
@check_figures_equal(extensions=["png"])
def test_savefig_transparent(fig_test, fig_ref):
# create two transparent subfigures with corresponding transparent inset
# axes. the entire background of the image should be transparent.
gs1 = fig_test.add_gridspec(3, 3, left=0.05, wspace=0.05)
f1 = fig_test.add_subfigure(gs1[:, :])
f2 = f1.add_subfigure(gs1[0, 0])

ax12 = f2.add_subplot(gs1[:, :])

ax1 = f1.add_subplot(gs1[:-1, :])
iax1 = ax1.inset_axes([.1, .2, .3, .4])
iax2 = iax1.inset_axes([.1, .2, .3, .4])

ax2 = fig_test.add_subplot(gs1[-1, :-1])
ax3 = fig_test.add_subplot(gs1[-1, -1])

for ax in [ax12, ax1, iax1, iax2, ax2, ax3]:
ax.set(xticks=[], yticks=[])
ax.spines[:].set_visible(False)


def test_figure_repr():
fig = plt.figure(figsize=(10, 20), dpi=10)
assert repr(fig) == "<Figure size 100x200 with 0 Axes>"
Expand Down