Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 13a6ecf

Browse filesBrowse files
authored
Merge pull request #24816 from chahak13/transparent_inset_axes
[FIX]: Make inset axes transparent on savefig(..., transparent=True)
2 parents 4df59dc + 0b3348a commit 13a6ecf
Copy full SHA for 13a6ecf

File tree

Expand file treeCollapse file tree

2 files changed

+51
-3
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+51
-3
lines changed

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+28-3Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3316,12 +3316,37 @@ def savefig(self, fname, *, transparent=None, **kwargs):
33163316

33173317
with ExitStack() as stack:
33183318
if transparent:
3319+
def _recursively_make_subfig_transparent(exit_stack, subfig):
3320+
exit_stack.enter_context(
3321+
subfig.patch._cm_set(
3322+
facecolor="none", edgecolor="none"))
3323+
for ax in subfig.axes:
3324+
exit_stack.enter_context(
3325+
ax.patch._cm_set(
3326+
facecolor="none", edgecolor="none"))
3327+
for sub_subfig in subfig.subfigs:
3328+
_recursively_make_subfig_transparent(
3329+
exit_stack, sub_subfig)
3330+
3331+
def _recursively_make_axes_transparent(exit_stack, ax):
3332+
exit_stack.enter_context(
3333+
ax.patch._cm_set(facecolor="none", edgecolor="none"))
3334+
for child_ax in ax.child_axes:
3335+
exit_stack.enter_context(
3336+
child_ax.patch._cm_set(
3337+
facecolor="none", edgecolor="none"))
3338+
for child_childax in ax.child_axes:
3339+
_recursively_make_axes_transparent(
3340+
exit_stack, child_childax)
3341+
33193342
kwargs.setdefault('facecolor', 'none')
33203343
kwargs.setdefault('edgecolor', 'none')
3344+
# set subfigure to appear transparent in printed image
3345+
for subfig in self.subfigs:
3346+
_recursively_make_subfig_transparent(stack, subfig)
3347+
# set axes to be transparent
33213348
for ax in self.axes:
3322-
stack.enter_context(
3323-
ax.patch._cm_set(facecolor='none', edgecolor='none'))
3324-
3349+
_recursively_make_axes_transparent(stack, ax)
33253350
self.canvas.print_figure(fname, **kwargs)
33263351

33273352
def ginput(self, n=1, timeout=30, show_clicks=True,

‎lib/matplotlib/tests/test_figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,29 @@ def test_savefig_preserve_layout_engine(tmp_path):
539539
assert fig.get_layout_engine()._compress
540540

541541

542+
@mpl.rc_context({"savefig.transparent": True})
543+
@check_figures_equal(extensions=["png"])
544+
def test_savefig_transparent(fig_test, fig_ref):
545+
# create two transparent subfigures with corresponding transparent inset
546+
# axes. the entire background of the image should be transparent.
547+
gs1 = fig_test.add_gridspec(3, 3, left=0.05, wspace=0.05)
548+
f1 = fig_test.add_subfigure(gs1[:, :])
549+
f2 = f1.add_subfigure(gs1[0, 0])
550+
551+
ax12 = f2.add_subplot(gs1[:, :])
552+
553+
ax1 = f1.add_subplot(gs1[:-1, :])
554+
iax1 = ax1.inset_axes([.1, .2, .3, .4])
555+
iax2 = iax1.inset_axes([.1, .2, .3, .4])
556+
557+
ax2 = fig_test.add_subplot(gs1[-1, :-1])
558+
ax3 = fig_test.add_subplot(gs1[-1, -1])
559+
560+
for ax in [ax12, ax1, iax1, iax2, ax2, ax3]:
561+
ax.set(xticks=[], yticks=[])
562+
ax.spines[:].set_visible(False)
563+
564+
542565
def test_figure_repr():
543566
fig = plt.figure(figsize=(10, 20), dpi=10)
544567
assert repr(fig) == "<Figure size 100x200 with 0 Axes>"

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.