diff --git a/control/tests/timeplot_test.py b/control/tests/timeplot_test.py index ea0a290c9..505be697a 100644 --- a/control/tests/timeplot_test.py +++ b/control/tests/timeplot_test.py @@ -370,6 +370,29 @@ def test_list_responses(resp_fcn): assert cplt.lines[row, col][1].get_color() == 'tab:orange' +@pytest.mark.parametrize("plot_inputs, shape", [ + (None, (2, 1)), (True, (2, 1)), ('overlay', (1, 1)), (False, (1, 1))]) +@pytest.mark.usefixtures('mplcleanup') +def test_time_response_plot_list(plot_inputs, shape): + # A list of responses is plotted like a TimeResponseList (gh-1171) + sys = ct.ss(ct.tf([1, 2], [3, 4, 5])) + timepts = np.linspace(0, 10) + resp1 = ct.input_output_response(sys, timepts, np.sin(timepts)) + resp2 = ct.input_output_response(sys, timepts, np.cos(timepts)) + + cplt = ct.time_response_plot([resp1, resp2], plot_inputs=plot_inputs) + assert cplt.lines.shape == shape + + plt.figure() + cplt_list = ct.TimeResponseList([resp1, resp2]).plot( + plot_inputs=plot_inputs) + assert cplt_list.lines.shape == shape + for lines, lines_list in zip(cplt.lines.flat, cplt_list.lines.flat): + assert len(lines) == len(lines_list) + for line, line_list in zip(lines, lines_list): + np.testing.assert_equal(line.get_ydata(), line_list.get_ydata()) + + @pytest.mark.slycot @pytest.mark.usefixtures('mplcleanup') def test_linestyles(): diff --git a/control/timeplot.py b/control/timeplot.py index 545618f75..1f0094f72 100644 --- a/control/timeplot.py +++ b/control/timeplot.py @@ -52,8 +52,9 @@ def time_response_plot( Parameters ---------- - data : `TimeResponseData` - Data to be plotted. + data : `TimeResponseData` or list of `TimeResponseData` + Data to be plotted. The responses in a list are plotted on the + same axes, as `TimeResponseList.plot` does. plot_inputs : bool or str, optional Sets how and where to plot the inputs: * False: don't plot the inputs @@ -175,6 +176,17 @@ def time_response_plot( """ from .ctrlplot import _process_ax_keyword, _process_line_labels + from .timeresp import TimeResponseList + + # Plot a list of responses one at a time, as `TimeResponseList.plot` does + if isinstance(data, (list, tuple)): + if label is not None: + kwargs['label'] = label + return TimeResponseList(data).plot( + *fmt, ax=ax, plot_inputs=plot_inputs, plot_outputs=plot_outputs, + transpose=transpose, overlay_traces=overlay_traces, + overlay_signals=overlay_signals, add_initial_zero=add_initial_zero, + trace_labels=trace_labels, title=title, relabel=relabel, **kwargs) # # Process keywords and set defaults