Skip to content

Bug in time_response_plot for multiple responses + improvements in input/output colors. #1171

@murrayrm

Description

@murrayrm

The following code should work, but generates an error:

import numpy as np
import control as ct

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))
ct.time_response_plot([resp1, resp2])

Output:

File ~/Library/CloudStorage/Dropbox/macosx/src/python-control/murrayrm/control/timeplot.py:210, in time_response_plot(data, ax, plot_inputs, plot_outputs, transpose, overlay_traces, overlay_signals, add_initial_zero, label, trace_labels, title, relabel, *fmt, **kwargs)
    208 # Determine whether or not to plot the input data (and how)
    209 if plot_inputs is None:
--> 210     plot_inputs = data.plot_inputs
    211 if plot_inputs not in [True, False, 'overlay']:
    212     raise ValueError(f"unrecognized value: {plot_inputs=}")

AttributeError: 'list' object has no attribute 'plot_inputs'

Attempting to fixing the error manually, I tried:

ct.time_response_plot([resp1, resp2], plot_inputs=True)

which generated another error:

File ~/Library/CloudStorage/Dropbox/macosx/src/python-control/murrayrm/control/timeplot.py:266, in time_response_plot(data, ax, plot_inputs, plot_outputs, transpose, overlay_traces, overlay_signals, add_initial_zero, label, trace_labels, title, relabel, *fmt, **kwargs)
    212     raise ValueError(f"unrecognized value: {plot_inputs=}")
    214 #
    215 # Find/create axes
    216 #
   (...)
    264 
    265 # Decide on the number of inputs and outputs
--> 266 ninputs = data.ninputs if plot_inputs else 0
    267 noutputs = data.noutputs if plot_outputs else 0
    268 ntraces = max(1, data.ntraces)      # treat data.ntraces == 0 as 1 trace

AttributeError: 'list' object has no attribute 'ninputs'

Those should be fairly easy to fix. While doing so, it might be nice to think through the color and labeling scheme as well. Using the following commands generates the desired plot, but the labeling is confusing:

resp1.plot()
resp2.plot()
Image

What I would have expected here is that the colors of the traces in the top and bottom panels would be the same, so that you could figure out which went with which.

Of course, there are other situations for which one wants different colors in the inputs and outputs, for example for a MIMO system:

sys_mimo = ct.rss(4, 2, 2)
resp = ct.input_output_response(sys_mimo, timepts, [np.sin(timepts), np.cos(timepts)])
resp.plot(overlay_signal=True)

which generates

Image

Here the output and input traces all have different colors because there is no reason to associate output $i$ with input $i$ in a general MIMO system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions