@@ -134,7 +134,8 @@ def bode_plot(
134134 If True, draw gain and phase margin lines on the magnitude and phase
135135 graphs and display the margins at the top of the graph. If set to
136136 'overlay', the values for the gain and phase margin are placed on
137- the graph. Setting display_margins turns off the axes grid.
137+ the graph. Setting `display_margins` turns off the axes grid, unless
138+ `grid` is explicitly set to True.
138139 **kwargs : `matplotlib.pyplot.plot` keyword properties, optional
139140 Additional keywords passed to `matplotlib` to specify line properties.
140141
@@ -276,6 +277,24 @@ def bode_plot(
276277# Make a copy of the kwargs dictionary since we will modify it
277278kwargs = dict (kwargs )
278279
280+ # Legacy keywords for margins
281+ display_margins = config ._process_legacy_keyword (
282+ kwargs ,'margins' ,'display_margins' ,display_margins )
283+ if kwargs .pop ('margin_info' ,False ):
284+ warnings .warn (
285+ "keyword 'margin_info' is deprecated; "
286+ "use 'display_margins='overlay'" )
287+ if display_margins is False :
288+ raise ValueError (
289+ "conflicting_keywords: `display_margins` and `margin_info`" )
290+
291+ # Turn off grid if display margins, unless explicitly overridden
292+ if display_margins and 'grid' not in kwargs :
293+ kwargs ['grid' ]= False
294+
295+ margins_method = config ._process_legacy_keyword (
296+ kwargs ,'method' ,'margins_method' ,margins_method )
297+
279298# Get values for params (and pop from list to allow keyword use in plot)
280299dB = config ._get_param (
281300'freqplot' ,'dB' ,kwargs ,_freqplot_defaults ,pop = True )
@@ -316,19 +335,6 @@ def bode_plot(
316335"sharex cannot be present with share_frequency" )
317336kwargs ['share_frequency' ]= sharex
318337
319- # Legacy keywords for margins
320- display_margins = config ._process_legacy_keyword (
321- kwargs ,'margins' ,'display_margins' ,display_margins )
322- if kwargs .pop ('margin_info' ,False ):
323- warnings .warn (
324- "keyword 'margin_info' is deprecated; "
325- "use 'display_margins='overlay'" )
326- if display_margins is False :
327- raise ValueError (
328- "conflicting_keywords: `display_margins` and `margin_info`" )
329- margins_method = config ._process_legacy_keyword (
330- kwargs ,'method' ,'margins_method' ,margins_method )
331-
332338if not isinstance (data , (list ,tuple )):
333339data = [data ]
334340
@@ -727,7 +733,7 @@ def _make_line_label(response, output_index, input_index):
727733label = '_nyq_mag_' + sysname )
728734
729735# Add a grid to the plot
730- ax_mag .grid (grid and not display_margins ,which = 'both' )
736+ ax_mag .grid (grid ,which = 'both' )
731737
732738# Phase
733739if plot_phase :
@@ -742,7 +748,7 @@ def _make_line_label(response, output_index, input_index):
742748label = '_nyq_phase_' + sysname )
743749
744750# Add a grid to the plot
745- ax_phase .grid (grid and not display_margins ,which = 'both' )
751+ ax_phase .grid (grid ,which = 'both' )
746752
747753#
748754# Display gain and phase margins (SISO only)
@@ -753,6 +759,10 @@ def _make_line_label(response, output_index, input_index):
753759raise NotImplementedError (
754760"margins are not available for MIMO systems" )
755761
762+ if display_margins == 'overlay' and len (data )> 1 :
763+ raise NotImplementedError (
764+ f"{ display_margins = } not supported for multi-trace plots" )
765+
756766# Compute stability margins for the system
757767margins = stability_margins (response ,method = margins_method )
758768gm ,pm ,Wcg ,Wcp = (margins [i ]for i in [0 ,1 ,3 ,4 ])
@@ -844,12 +854,12 @@ def _make_line_label(response, output_index, input_index):
844854
845855else :
846856# Put the title underneath the suptitle (one line per system)
847- ax = ax_mag if ax_mag else ax_phase
848- axes_title = ax .get_title ()
857+ ax_ = ax_mag if ax_mag else ax_phase
858+ axes_title = ax_ .get_title ()
849859if axes_title is not None and axes_title != "" :
850860axes_title += "\n "
851861with plt .rc_context (rcParams ):
852- ax .set_title (
862+ ax_ .set_title (
853863axes_title + f"{ sysname } : "
854864"Gm = %.2f %s(at %.2f %s), "
855865"Pm = %.2f %s (at %.2f %s)" %