File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -898,6 +898,18 @@ def draw(self, renderer):
898898 self ._apply_share_label_groups ()
899899 super ().draw (renderer )
900900
901+ @override
902+ def draw_without_rendering (self ):
903+ """
904+ Draw without output while preserving figure dpi state.
905+ """
906+ dpi = self .dpi
907+ try :
908+ return super ().draw_without_rendering ()
909+ finally :
910+ if self .dpi != dpi :
911+ mfigure .Figure .set_dpi (self , dpi )
912+
901913 def _is_auto_share_mode (self , which : str ) -> bool :
902914 """Return whether a given axis uses auto-share mode."""
903915 if which not in ("x" , "y" ):
Original file line number Diff line number Diff line change @@ -78,6 +78,21 @@ def test_get_renderer_basic():
7878 assert hasattr (renderer , "draw_path" )
7979
8080
81+ def test_draw_without_rendering_preserves_dpi ():
82+ """
83+ draw_without_rendering should not mutate figure dpi/bbox.
84+ """
85+ fig , ax = uplt .subplots (figsize = (4 , 3 ), dpi = 101 )
86+ dpi_before = fig .dpi
87+ bbox_before = np .array ([fig .bbox .width , fig .bbox .height ])
88+
89+ fig .draw_without_rendering ()
90+
91+ assert np .isclose (fig .dpi , dpi_before )
92+ assert np .allclose ([fig .bbox .width , fig .bbox .height ], bbox_before )
93+ uplt .close (fig )
94+
95+
8196def test_figure_sharing_toggle ():
8297 """
8398 Check if axis sharing and unsharing works
You can’t perform that action at this time.
0 commit comments