diff --git a/Project.toml b/Project.toml index 2ec7651..f424e55 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ControlPlots" uuid = "23c2ee80-7a9e-4350-b264-8e670f12517c" authors = ["Uwe Fechner and contributors"] -version = "0.0.2" +version = "0.0.3" [deps] JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" diff --git a/README.md b/README.md index 66063bc..2f6f414 100644 --- a/README.md +++ b/README.md @@ -97,3 +97,14 @@ X = sin.(T) Y = cos.(T) p = plotxy(X, Y, xlabel="X", ylabel="Y", fig="xy") ``` + +### n-in-one Plot +You can plot multiple time series in one plot, e.g. like this: +```julia +using ControlPlots + +x = 1.5*ones(11) +y = 1:0.1:2 +out = min.(x, y) +plot(1:11, [x, y, out]; labels=["input_a", "input_b", "output"]) +``` diff --git a/examples/two_in_one.jl b/examples/two_in_one.jl index e6b97ec..09cfa65 100644 --- a/examples/two_in_one.jl +++ b/examples/two_in_one.jl @@ -1,9 +1,5 @@ using ControlPlots -x=1.5*ones(11) -y=1:0.1:2 -out=min.(x,y) -plt.plot(1:11, x, label="input_a") -plt.plot(1:11, y, label="input_b") -plt.plot(1:11, out, label="output") -plt.grid(true) -plt.legend() +x = 1.5*ones(11) +y = 1:0.1:2 +out = min.(x,y) +plot(1:11, [x,y,out]; labels=["input_a", "input_b", "output"]) diff --git a/src/ControlPlots.jl b/src/ControlPlots.jl index 4009368..89f0f0c 100644 --- a/src/ControlPlots.jl +++ b/src/ControlPlots.jl @@ -10,6 +10,7 @@ export plot, plotx, plotxy, plt, load, save mutable struct PlotX X Y + labels xlabel ylabels fig @@ -24,6 +25,38 @@ function load(filename::String) JLD2.load(filename)["plot"] end +function plot(X, Ys::Vector{AbstractVector{Float64}}; xlabel="", ylabel="", + labels=nothing, fig="", disp=false) + if disp + if fig != "" + plt.figure(fig) + end + for (i, Y) in pairs(Ys) + if isnothing(labels) + plt.plot(X, Y) + else + if isnothing(labels[i]) || labels[i]=="" + plt.plot(X, Y) + else + plt.plot(X, Y; label=labels[i]) + end + end + end + if xlabel != "" + plt.xlabel(xlabel, fontsize=14); + end + if ylabel != "" + plt.ylabel(ylabel, fontsize=14); + end + plt.grid(true) + plt.legend() + plt.tight_layout() + else + println("OK") + end + PlotX(X, Ys, labels, xlabel, ylabel, fig, 4) +end + function plot(X, Y; xlabel="", ylabel="", fig="", disp=false) if disp if fig != "" @@ -37,7 +70,7 @@ function plot(X, Y; xlabel="", ylabel="", fig="", disp=false) plt.grid(true) plt.tight_layout() end - PlotX(X, Y, xlabel, ylabel, fig, 1) + PlotX(X, Y, nothing, xlabel, ylabel, fig, 1) end function plotx(X, Y...; xlabel="time [s]", ylabels=nothing, fig="", title="", disp=false) @@ -74,7 +107,7 @@ function plotx(X, Y...; xlabel="time [s]", ylabels=nothing, fig="", title="", di plt.tight_layout() end - PlotX(collect(X), Y, xlabel, ylabels, fig, 2) + PlotX(collect(X), Y, nothing, xlabel, ylabels, fig, 2) end function plotxy(X, Y; xlabel="", ylabel="", fig="", disp=false) @@ -88,7 +121,7 @@ function plotxy(X, Y; xlabel="", ylabel="", fig="", disp=false) plt.grid(true) plt.tight_layout() end - PlotX(X, Y, xlabel, ylabel, fig, 3) + PlotX(X, Y, xlabel, nothing, ylabel, fig, 3) end function display(P::PlotX) @@ -96,8 +129,10 @@ function display(P::PlotX) plot(P.X, P.Y; xlabel=P.xlabel, ylabel=P.ylabels, fig=P.fig, disp=true) elseif P.type == 2 plotx(P.X, P.Y...; xlabel=P.xlabel, ylabels=P.ylabels, fig=P.fig, disp=true) - else + elseif P.type == 3 plotxy(P.X, P.Y; xlabel=P.xlabel, ylabel=P.ylabels, fig=P.fig, disp=true) + else + plot(P.X, P.Y; xlabel=P.xlabel, labels=P.labels, fig=P.fig, disp=true) end plt.pause(0.01) plt.show(block=false)