From 0908973e108e945533b02f6ddf1f1227fbbf9b66 Mon Sep 17 00:00:00 2001 From: Ruth Comer <10599679+rcomer@users.noreply.github.com> Date: Sun, 15 Dec 2024 20:05:02 +0000 Subject: [PATCH] FIX: pass renderer through adjust_bbox --- lib/matplotlib/_tight_bbox.py | 8 ++++---- lib/matplotlib/backend_bases.py | 5 ++++- lib/matplotlib/backends/backend_mixed.py | 11 +++++++---- .../bbox_inches_inset_rasterized.pdf | Bin 0 -> 3487 bytes lib/matplotlib/tests/test_bbox_tight.py | 16 ++++++++++++++++ 5 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_inset_rasterized.pdf diff --git a/lib/matplotlib/_tight_bbox.py b/lib/matplotlib/_tight_bbox.py index db72bbdff020..7cd28dd031e6 100644 --- a/lib/matplotlib/_tight_bbox.py +++ b/lib/matplotlib/_tight_bbox.py @@ -5,7 +5,7 @@ from matplotlib.transforms import Bbox, TransformedBbox, Affine2D -def adjust_bbox(fig, bbox_inches, fixed_dpi=None): +def adjust_bbox(fig, bbox_inches, renderer, fixed_dpi=None): """ Temporarily adjust the figure so that only the specified area (bbox_inches) is saved. @@ -25,7 +25,7 @@ def adjust_bbox(fig, bbox_inches, fixed_dpi=None): for ax in fig.axes: locator = ax.get_axes_locator() if locator is not None: - ax.apply_aspect(locator(ax, None)) + ax.apply_aspect(locator(ax, renderer)) locator_list.append(locator) current_pos = ax.get_position(original=False).frozen() ax.set_axes_locator(lambda a, r, _pos=current_pos: _pos) @@ -70,7 +70,7 @@ def restore_bbox(): return restore_bbox -def process_figure_for_rasterizing(fig, bbox_inches_restore, fixed_dpi=None): +def process_figure_for_rasterizing(fig, bbox_inches_restore, renderer, fixed_dpi=None): """ A function that needs to be called when figure dpi changes during the drawing (e.g., rasterizing). It recovers the bbox and re-adjust it with @@ -79,6 +79,6 @@ def process_figure_for_rasterizing(fig, bbox_inches_restore, fixed_dpi=None): bbox_inches, restore_bbox = bbox_inches_restore restore_bbox() - r = adjust_bbox(fig, bbox_inches, fixed_dpi) + r = adjust_bbox(fig, bbox_inches, renderer, fixed_dpi) return bbox_inches, r diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 9b9cfe3ccc76..0b1bc77bcab7 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2153,6 +2153,9 @@ def print_figure( # so that we can inject the orientation with getattr(renderer, "_draw_disabled", nullcontext)(): self.figure.draw(renderer) + else: + renderer = None + if bbox_inches: if bbox_inches == "tight": bbox_inches = self.figure.get_tightbbox( @@ -2169,7 +2172,7 @@ def print_figure( # call adjust_bbox to save only the given area restore_bbox = _tight_bbox.adjust_bbox( - self.figure, bbox_inches, self.figure.canvas.fixed_dpi) + self.figure, bbox_inches, renderer, self.figure.canvas.fixed_dpi) _bbox_inches_restore = (bbox_inches, restore_bbox) else: diff --git a/lib/matplotlib/backends/backend_mixed.py b/lib/matplotlib/backends/backend_mixed.py index 6571d1928c0d..ea8f50a39ad1 100644 --- a/lib/matplotlib/backends/backend_mixed.py +++ b/lib/matplotlib/backends/backend_mixed.py @@ -75,15 +75,17 @@ def start_rasterizing(self): """ # change the dpi of the figure temporarily. self.figure.dpi = self.dpi - if self._bbox_inches_restore: # when tight bbox is used - r = process_figure_for_rasterizing(self.figure, - self._bbox_inches_restore) - self._bbox_inches_restore = r self._raster_renderer = self._raster_renderer_class( self._width*self.dpi, self._height*self.dpi, self.dpi) self._renderer = self._raster_renderer + if self._bbox_inches_restore: # when tight bbox is used + r = process_figure_for_rasterizing(self.figure, + self._bbox_inches_restore, + self._raster_renderer) + self._bbox_inches_restore = r + def stop_rasterizing(self): """ Exit "raster" mode. All of the drawing that was done since @@ -115,5 +117,6 @@ def stop_rasterizing(self): if self._bbox_inches_restore: # when tight bbox is used r = process_figure_for_rasterizing(self.figure, self._bbox_inches_restore, + self._vector_renderer, self._figdpi) self._bbox_inches_restore = r diff --git a/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_inset_rasterized.pdf b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_inset_rasterized.pdf new file mode 100644 index 0000000000000000000000000000000000000000..17a80ef876d12c97109679908ee081b257d835df GIT binary patch literal 3487 zcmcIndsGuw8dvx1YNK1ail8nF7kPyunVBR&LIEK_9*a^SfUJo^9ur7U$UrhgM8s<4 zm6uh}-J)F(>jI)~+10ipK1;eTdK5h(^eCcG)E=Qe>Wa4OcJCwvLD+K^_iXaVo%!aQ zdw=))zTfYDwHUBCuv09BlFiDBKi!9S+es z;3Fxxo+!{M;C?9laQ&6IQZ3^Vg>Xd_{+S#$1I3tQ;~E$TxH&k39&jBHW3*ZV-eHvc z_<&H4oe^AtYqQB504Jh+5WZRi_Y6l33`88RAe1--T~F$8SuU+`k42oIorTToz3X~1 zI#T3crQDU|n`)?zcX^*4ykvFqC%PBmnJ%QU_1sr!i;#`gZcV2mAXV z{sq_MzigPlA|-?uT^UvGYwE22(~xE*J+}PzF*(Cy_z3d1#a-gA-?Beu54Oow&)g0T z%vrdqOxqN`vElv#+MPvBDZz#-H4kZ5=LzQ3H073l_^aCHi+hiBsCcD5Wtz*MpJ140 zeRNsm>t6fD&-X`UE$lMv`*QngZ953<{(+l#ubi{)NqVB``L;P}Z9&4!rAL<+&(_r$ z+TVOi+u(k_d+~uiT|U=OhYqJ&rE9*f{;6YmyD7A?`yV~!cjXC7hC&R90W zwCjeQtN>Z1Rze1_!r&%^xH>xr3Pc!6gKW|eI&q#%fm0k8X_a^(t^`ONZcr=m6af!# zxitEVyS{IKS|T{DeKhDH^wxbdu&8ff5g#kLblbzLzU*w@Qh}TLXm3!%byn@y96`Xk z9o;eOFDJ*dH|Z{=W&J%%_!~mtBInOt8#gnR7r8YirYt(Pe7T?^N>mwnoaCmNIIGMd zpBJ(-EzF}uXto7WTY?GevV5yFaJ^MZ-(U^cX!UE}>|1K_DYq=Gw0Jjc1>4PDRVL4m zj6O|UxW^2FprU7YycvHf9{OfUcmLv!Tb?Iw`twfaAG(3o^h8*$#g$zZm0U^8y_k`G zK`Qn9xI%JH&JQk5`%0DEo)Z_gIpM5ExI`nEkH^oE#D;(1Sz8ggqSD)HekaKoo?;4J zZ4Qx2%}lukQ(71rYY=ID+W>(b-X!!P{ol9xZLxZrE#Q+Ap&>jq zsZYXnu|#ejp`|pBGo|v-V6Z6#g2I@qw-;5C4vL_HBy^OhIWh$G&3;votJOP_IK}K# zT{@M{F6R?OZMZWn-7RZI^qIcz+P`y~t7(zU8%iH~@!(AGjk^ynER*>kH*T_>muI)^ z%b;Idh!}@$kLC5!W4ghPok6P(PxIn2*5PhiY1`Q^YB|ueIwEN8!sDjM($|{{zBN0r zPcjtr)SWP{v;EkgmEZU*sd!oS^kF#wLrZ~vnj!utLpArcazG(; zNhxXKh>MY3uHD9cw%h%>{JJmF=+jQ;=w^#Qe7xWGeZLyZj9`4W(8V;{mfEsEX}Y!H zjNEfDIAYph>y_*C8+tM{bglb+)9(MJMlvNg=;`j=rkysU{BTCPG}yn=xz@DPQBMEK zrS+KR;S?v0Q_49^w)0f6EAupkcua>e4Ery1m=#K+^G_2G20eNQ7IobAYP`-?oX>4jc3#@3rs`p%x{tydHDo#{fi z96nkS&)LF@E?X|Fj7Tztw;DJZW=v&8jucA+i>%&Oi&wSz|1D%-DwQ0sV~#!)TE?2Q z;SSr()8MTcXH4%TS1cGl#V@*b;(O1)$9})Qmb?67b6fKdX~QGiHg-p(Ur~mJ|FIr@ z%gtnyw`43E9kvb5II#&_n1dN$x2@8`QHrd8yPkNVyGRMV=)XCHuTxb81603`F7<+)E5CoMi``3U-t0DkKWy}8g WpeJQI(w-(3%I47g{Nmr`)BX)JDL7jI literal 0 HcmV?d00001 diff --git a/lib/matplotlib/tests/test_bbox_tight.py b/lib/matplotlib/tests/test_bbox_tight.py index 5f2b397b650d..089b3a63005b 100644 --- a/lib/matplotlib/tests/test_bbox_tight.py +++ b/lib/matplotlib/tests/test_bbox_tight.py @@ -8,6 +8,7 @@ import matplotlib.path as mpath import matplotlib.patches as mpatches from matplotlib.ticker import FuncFormatter +from mpl_toolkits.axes_grid1.inset_locator import inset_axes @image_comparison(['bbox_inches_tight'], remove_text=True, @@ -173,3 +174,18 @@ def test_bbox_inches_fixed_aspect(): ax.plot([0, 1]) ax.set_xlim(0, 1) ax.set_aspect('equal') + + +@image_comparison(['bbox_inches_inset_rasterized'], extensions=['pdf'], + remove_text=True, savefig_kwarg={'bbox_inches': 'tight'}, + style='mpl20') +def test_bbox_inches_inset_rasterized(): + fig, ax = plt.subplots() + + arr = np.arange(100).reshape(10, 10) + im = ax.imshow(arr) + inset = inset_axes( + ax, width='10%', height='30%', loc='upper left', + bbox_to_anchor=(0.045, 0., 1, 1), bbox_transform=ax.transAxes) + + fig.colorbar(im, cax=inset, orientation='horizontal')