ControlSystems.jl: Custom labels in a plot of step response of a MIMO system

I am struggling with setting my own labels correctly in a plot of a step response of a MIMO system.

Consider a generic MIMO LTI system with 2 inputs and 2 outputs, say:

using ControlSystems
using Plots
A = [-10 2; 3 -40]
B = [5 6; 7 8]
C = [9 10; 11 12]
G = ss(A, B, C, 0)

We compute the step response using the step function. When plotting it, I want to set my own ylabels and my own labels. I do it by substituting a row matrix of strings.

step_response = step(G, 10.0)
plot(step_response, title="", xlabel="Time (s)", ylabel=["y₁" "y₂"], lw=2, label=["From u₁" "From u₂"], legend=:topright)

While this works for the ylabel, it does not for the label. What I get is

Setting label=["From u₁", "From u₂"] does not help.

I can do it by using a named system as in

using RobustAndOptimalControl
G = named_ss(G, u = [:u₁, :u₂], y = [:y₁, :y₂])
step_response = step(G, 10.0)
plot(step_response, title="", xlabel="Time (s)", lw=2)

But still, is it possible to do it without the named system, just by manually overwriting the labels in the plot?

plot(step_response, title="", xlabel="Time (s)", ylabel=["y₁" "y₂"], lw=2, label=["From u₁" "From u₁" "From u₂" "From u₂"], legend=:bottomright)
1 Like

The plot command expects the labels in the order the series are drawn in the plot recipe (which should ideally be an implementation detail only), and each series requires its own label. It’s unfortunately not trivial to intercept keywords or group multiple series together in a way that isn’t easily broken, so what I usually recommend when customizing plots turns out to be difficult is to split the plotting up into multiple plot commands.

This PR could potentially make it easier to customize the labels, in the limited case where you only want to change the name of the signals:

1 Like