Skip to content

Commit c59a7fa

Browse files
sramesh750chahak13
andcommitted
FIX: make inset axes transparent on figures
Co-authored-by: Shreeya Ramesh <shreeyar@umich.edu> Co-authored-by: Chahak Mehta <chahakmehta013@gmail.com>
1 parent 647afe6 commit c59a7fa

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

lib/matplotlib/figure.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,12 +3059,37 @@ def savefig(self, fname, *, transparent=None, **kwargs):
30593059

30603060
with ExitStack() as stack:
30613061
if transparent:
3062+
def _recursively_make_subfig_transparent(exit_stack, subfig):
3063+
exit_stack.enter_context(
3064+
subfig.patch._cm_set(
3065+
facecolor="none", edgecolor="none"))
3066+
for ax in subfig.axes:
3067+
exit_stack.enter_context(
3068+
ax.patch._cm_set(
3069+
facecolor="none", edgecolor="none"))
3070+
for sub_subfig in subfig.subfigs:
3071+
_recursively_make_subfig_transparent(
3072+
exit_stack, sub_subfig)
3073+
3074+
def _recursively_make_axes_transparent(exit_stack, ax):
3075+
exit_stack.enter_context(
3076+
ax.patch._cm_set(facecolor="none", edgecolor="none"))
3077+
for child_ax in ax.child_axes:
3078+
exit_stack.enter_context(
3079+
child_ax.patch._cm_set(
3080+
facecolor="none", edgecolor="none"))
3081+
for child_childax in ax.child_axes:
3082+
_recursively_make_axes_transparent(
3083+
exit_stack, child_childax)
3084+
30623085
kwargs.setdefault('facecolor', 'none')
30633086
kwargs.setdefault('edgecolor', 'none')
3087+
# set subfigure to appear transparent in printed image
3088+
for subfig in self.subfigs:
3089+
_recursively_make_subfig_transparent(stack, subfig)
3090+
# set axes to be transparent
30643091
for ax in self.axes:
3065-
stack.enter_context(
3066-
ax.patch._cm_set(facecolor='none', edgecolor='none'))
3067-
3092+
_recursively_make_axes_transparent(stack, ax)
30683093
self.canvas.print_figure(fname, **kwargs)
30693094

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

lib/matplotlib/tests/test_figure.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,33 @@ def test_savefig_pixel_ratio(backend):
557557
assert ratio1 == ratio2
558558

559559

560+
@image_comparison(['transparent_background'],
561+
extensions=['png'], savefig_kwarg={'transparent': True},
562+
remove_text=True)
563+
def test_savefig_transparent():
564+
# create two transparent subfigures with corresponding transparent inset
565+
# axes. the entire background of the image should be transparent.
566+
fig = plt.gcf()
567+
568+
gs1 = fig.add_gridspec(3, 3, left=0.05, wspace=0.05)
569+
f1 = fig.add_subfigure(gs1[:, :])
570+
f2 = f1.add_subfigure(gs1[0, 0])
571+
ax12 = f2.add_subplot(gs1[:, :])
572+
ax12.set(xticks=[], yticks=[])
573+
574+
ax1 = f1.add_subplot(gs1[:-1, :])
575+
ax1.set(xticks=[], yticks=[])
576+
iax1 = ax1.inset_axes([.1, .2, .3, .4])
577+
iax1.set(xticks=[], yticks=[])
578+
iax2 = iax1.inset_axes([.1, .2, .3, .4])
579+
iax2.set(xticks=[], yticks=[])
580+
581+
ax2 = fig.add_subplot(gs1[-1, :-1])
582+
ax2.set(xticks=[], yticks=[])
583+
ax3 = fig.add_subplot(gs1[-1, -1])
584+
ax3.set(xticks=[], yticks=[])
585+
586+
560587
def test_figure_repr():
561588
fig = plt.figure(figsize=(10, 20), dpi=10)
562589
assert repr(fig) == "<Figure size 100x200 with 0 Axes>"

0 commit comments

Comments
 (0)