@@ -50,7 +50,8 @@ def time_response_plot(
5050 data , * fmt , ax = None , plot_inputs = None , plot_outputs = True ,
5151 transpose = False , overlay_traces = False , overlay_signals = False ,
5252 legend_map = None , legend_loc = None , add_initial_zero = True , label = None ,
53- trace_labels = None , title = None , relabel = True , ** kwargs ):
53+ trace_labels = None , title = None , relabel = True , show_legend = None ,
54+ ** kwargs ):
5455 """Plot the time response of an input/output system.
5556
5657 This function creates a standard set of plots for the input/output
@@ -131,6 +132,10 @@ def time_response_plot(
131132 relabel : bool, optional
132133 By default, existing figures and axes are relabeled when new data
133134 are added. If set to `False`, just plot new data on existing axes.
135+ show_legend : bool, optional
136+ Force legend to be shown if ``True`` or hidden if ``False``. If
137+ ``None``, then show legend when there is more than one line on an
138+ axis or ``legend_loc`` or ``legend_map`` have been specified.
134139 time_label : str, optional
135140 Label to use for the time axis.
136141 trace_props : array of dicts
@@ -565,6 +570,9 @@ def _make_line_label(signal_index, signal_labels, trace_index):
565570 legend_map = np .full (ax_array .shape , None , dtype = object )
566571 if legend_loc == None :
567572 legend_loc = 'center right'
573+ else :
574+ show_legend = True if show_legend is None else show_legend
575+
568576 if transpose :
569577 if (overlay_signals or plot_inputs == 'overlay' ) and overlay_traces :
570578 # Put a legend in each plot for inputs and outputs
@@ -611,6 +619,14 @@ def _make_line_label(signal_index, signal_labels, trace_index):
611619 else :
612620 # Put legend in the upper right
613621 legend_map [0 , - 1 ] = legend_loc
622+ else :
623+ # Make sure the legend map is the right size
624+ legend_map = np .atleast_2d (legend_map )
625+ if legend_map .shape != ax_array .shape :
626+ raise ValueError ("legend_map shape just match axes shape" )
627+
628+ # Turn legend on unless overridden by user
629+ show_legend = True if show_legend is None else show_legend
614630
615631 # Create axis legends
616632 for i in range (nrows ):
@@ -621,7 +637,9 @@ def _make_line_label(signal_index, signal_labels, trace_index):
621637 labels = _make_legend_labels (labels , plot_inputs == 'overlay' )
622638
623639 # Update the labels to remove common strings
624- if len (labels ) > 1 and legend_map [i , j ] != None :
640+ if show_legend != False and \
641+ (len (labels ) > 1 or show_legend ) and \
642+ legend_map [i , j ] != None :
625643 with plt .rc_context (rcParams ):
626644 ax .legend (labels , loc = legend_map [i , j ])
627645
0 commit comments