@@ -163,23 +163,38 @@ def try_render_matplotlib(
163163 # (if there is one) adjusts the figure's subplot parameters.
164164 # e.g. "tight" layout.
165165 # When there is no layout engine, "tight" layout is often helpful
166- layout_engine = fig .get_layout_engine ()
167- if layout_engine :
168- if not layout_engine .adjust_compatible :
169- # In most cases, this branch will override the constained layout.
170- # which is usually a very deliberate choice by the user
166+ layout_engine = None
167+ # get_layout_engine was added in matplotlib 3.6
168+ if hasattr (fig , "get_layout_engine" ):
169+ layout_engine = fig .get_layout_engine ()
170+ if layout_engine :
171+ if not layout_engine .adjust_compatible :
172+ # In most cases, this branch will override the constained layout.
173+ # which is usually a very deliberate choice by the user
174+ fig .set_layout_engine ( # pyright: ignore[reportUnknownMemberType]
175+ layout = "tight"
176+ )
177+ warnings .warn (
178+ f"'{ type (layout_engine )} ' layout engine is not compatible with shiny. "
179+ "The figure layout has been changed to tight." ,
180+ stacklevel = 1 ,
181+ )
182+ else :
171183 fig .set_layout_engine ( # pyright: ignore[reportUnknownMemberType]
172184 layout = "tight"
173185 )
174- warnings .warn (
175- f"'{ type (layout_engine )} ' layout engine is not compatible with shiny. "
176- "The figure layout has been changed to tight." ,
177- stacklevel = 1 ,
178- )
179186 else :
180- fig .set_layout_engine ( # pyright: ignore[reportUnknownMemberType]
181- layout = "tight"
182- )
187+ # This branch needed for matplotlib <3.6. Eventually we will be able to
188+ # remove this code path.
189+
190+ # Suppress the message `UserWarning: The figure layout has changed to tight`
191+ with warnings .catch_warnings ():
192+ warnings .filterwarnings (
193+ action = "ignore" ,
194+ category = UserWarning ,
195+ message = "The figure layout has changed to tight" ,
196+ )
197+ plt .tight_layout () # pyright: ignore[reportUnknownMemberType]
183198
184199 with io .BytesIO () as buf :
185200 fig .savefig ( # pyright: ignore[reportUnknownMemberType]
0 commit comments