Skip to content

Commit cc70a86

Browse files
sramesh750anntzer
authored andcommitted
FIX: savefig)...,transparent=True) now makes inset_axes transparent as well
FIX: Indented child axes to be inside previous loop to fix local variable error Added test for setting transparent inset axes in savefig function, added code to savefig to ensure that subfigures also made transparent + corresponding test Added baseline images corresponding to newly added tests for figure.py Changed test_savefig_transparent_subfigure to add subplots to subfigures created so that baseline image is not completely blank Combined test_savefig_transparent_inset_axes and test_savefig_transparent_subfigure into one test: test_savefig_transparent and added remove_text=True to image_comparison wrapper function Removed older baseline images that we no longer need Fixed flake8 issues with test_figure.py
1 parent 647afe6 commit cc70a86

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

lib/matplotlib/figure.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -3061,10 +3061,18 @@ def savefig(self, fname, *, transparent=None, **kwargs):
30613061
if transparent:
30623062
kwargs.setdefault('facecolor', 'none')
30633063
kwargs.setdefault('edgecolor', 'none')
3064+
for subfig in self.subfigs:
3065+
subfig.set_facecolor('none')
3066+
subfig.set_edgecolor('none')
3067+
# set axes to be transparent
30643068
for ax in self.axes:
30653069
stack.enter_context(
30663070
ax.patch._cm_set(facecolor='none', edgecolor='none'))
3067-
3071+
# set inset axes to be transparent as well
3072+
for child_ax in ax.child_axes:
3073+
stack.enter_context(
3074+
child_ax.patch._cm_set(
3075+
facecolor='none', edgecolor='none'))
30683076
self.canvas.print_figure(fname, **kwargs)
30693077

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

lib/matplotlib/tests/test_figure.py

+18
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,24 @@ 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+
gs = fig.add_gridspec(3, 3)
568+
fig.add_subfigure(gs[0, 0])
569+
fig.add_subplot(gs[0, 0])
570+
ax = plt.gca()
571+
ax.inset_axes([.1, .2, .3, .4])
572+
fig.add_subfigure(gs[0:2, 1])
573+
fig.add_subplot(gs[0:2, 1])
574+
ax2 = plt.gca()
575+
ax2.inset_axes([.1, .2, .3, .4])
576+
577+
560578
def test_figure_repr():
561579
fig = plt.figure(figsize=(10, 20), dpi=10)
562580
assert repr(fig) == "<Figure size 100x200 with 0 Axes>"

0 commit comments

Comments
 (0)