@@ -50,7 +50,8 @@ def time_response_plot(
50
50
data ,* fmt ,ax = None ,plot_inputs = None ,plot_outputs = True ,
51
51
transpose = False ,overlay_traces = False ,overlay_signals = False ,
52
52
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 ):
54
55
"""Plot the time response of an input/output system.
55
56
56
57
This function creates a standard set of plots for the input/output
@@ -131,6 +132,10 @@ def time_response_plot(
131
132
relabel : bool, optional
132
133
By default, existing figures and axes are relabeled when new data
133
134
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.
134
139
time_label : str, optional
135
140
Label to use for the time axis.
136
141
trace_props : array of dicts
@@ -565,6 +570,9 @@ def _make_line_label(signal_index, signal_labels, trace_index):
565
570
legend_map = np .full (ax_array .shape ,None ,dtype = object )
566
571
if legend_loc == None :
567
572
legend_loc = 'center right'
573
+ else :
574
+ show_legend = True if show_legend is None else show_legend
575
+
568
576
if transpose :
569
577
if (overlay_signals or plot_inputs == 'overlay' )and overlay_traces :
570
578
# Put a legend in each plot for inputs and outputs
@@ -611,6 +619,14 @@ def _make_line_label(signal_index, signal_labels, trace_index):
611
619
else :
612
620
# Put legend in the upper right
613
621
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
614
630
615
631
# Create axis legends
616
632
for i in range (nrows ):
@@ -621,7 +637,9 @@ def _make_line_label(signal_index, signal_labels, trace_index):
621
637
labels = _make_legend_labels (labels ,plot_inputs == 'overlay' )
622
638
623
639
# 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 :
625
643
with plt .rc_context (rcParams ):
626
644
ax .legend (labels ,loc = legend_map [i ,j ])
627
645