From e64af6d64d92e35575c3daaed07f0f2b97118e0f Mon Sep 17 00:00:00 2001 From: Shreeya Ramesh Date: Sat, 9 Apr 2022 21:26:06 -0400 Subject: [PATCH] FIX: savefig(...,transparent=True) now makes inset_axes transparent --- lib/matplotlib/figure.py | 16 +++++++++++++++- .../test_figure/transparent_background.png | Bin 0 -> 4960 bytes lib/matplotlib/tests/test_figure.py | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib/matplotlib/tests/baseline_images/test_figure/transparent_background.png diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index bd6e99de0350..a1e28e63be55 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -3061,10 +3061,24 @@ def savefig(self, fname, *, transparent=None, **kwargs): if transparent: kwargs.setdefault('facecolor', 'none') kwargs.setdefault('edgecolor', 'none') + # set subfigure to appear transparent in printed image + for subfig in self.subfigs: + stack.enter_context( + subfig.patch._cm_set( + facecolor='none', edgecolor='none')) + for subfig_ax in subfig.axes: + stack.enter_context( + subfig_ax.patch._cm_set( + facecolor='none', edgecolor='none')) + # set axes to be transparent for ax in self.axes: stack.enter_context( ax.patch._cm_set(facecolor='none', edgecolor='none')) - + # set inset axes to be transparent as well + for child_ax in ax.child_axes: + stack.enter_context( + child_ax.patch._cm_set( + facecolor='none', edgecolor='none')) self.canvas.print_figure(fname, **kwargs) def ginput(self, n=1, timeout=30, show_clicks=True, diff --git a/lib/matplotlib/tests/baseline_images/test_figure/transparent_background.png b/lib/matplotlib/tests/baseline_images/test_figure/transparent_background.png new file mode 100644 index 0000000000000000000000000000000000000000..4fa6440e78c20dcae125beaa8c5b86e4afd078c0 GIT binary patch literal 4960 zcmeH~YgAKL7RS#GQAk?}ptY14A){2CmIea~L5sn+j0LQ4c?p7s$6&-n5(p9!qNQUU zQ&Gneg($0C9gxc-C;bv9>9^I_R7Qz6~WMhYh*7DH+34;aTb9({+egXgi zunlVifZaSl0QinmOUE)GY;$h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by5Aw%aBPiojZ zmNj<#j1jC&o9n z`@nsit(q-eVoPt@_sy)}?rV-KhA&2IW66oOb?kCfg<%pRr*flE943;B3VKUSS za89JQl0_PwZnr>}`e?STcGGCIl_`m5h0*4xQI2Qj)v550^6_AcaI)K*vi6fQ5bnpO zC(t|3jhkyRE;bc|jh*p6tD(LzH_)20NMfz0_=VNoYqIhBE_4E&#Fz(f&O2CmW=_dG(O3;fpvt}b60yZ7IFs&@7O literal 0 HcmV?d00001 diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 9f68f63e5236..29cd33115601 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -557,6 +557,24 @@ def test_savefig_pixel_ratio(backend): assert ratio1 == ratio2 +@image_comparison(['transparent_background'], + extensions=['png'], savefig_kwarg={'transparent': True}, + remove_text=True) +def test_savefig_transparent(): + # create two transparent subfigures with corresponding transparent inset + # axes. the entire background of the image should be transparent. + fig = plt.gcf() + gs = fig.add_gridspec(3, 3) + fig.add_subfigure(gs[0, 0]) + fig.add_subplot(gs[0, 0]) + ax = plt.gca() + ax.inset_axes([.1, .2, .3, .4]) + fig.add_subfigure(gs[0:2, 1]) + fig.add_subplot(gs[0:2, 1]) + ax2 = plt.gca() + ax2.inset_axes([.1, .2, .3, .4]) + + def test_figure_repr(): fig = plt.figure(figsize=(10, 20), dpi=10) assert repr(fig) == "
"