Skip to content

Commit

Permalink
Add n-in-one plot
Browse files Browse the repository at this point in the history
  • Loading branch information
Uwe Fechner committed Apr 4, 2024
1 parent 02238fd commit f9f2124
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ControlPlots"
uuid = "23c2ee80-7a9e-4350-b264-8e670f12517c"
authors = ["Uwe Fechner <[email protected]> and contributors"]
version = "0.0.2"
version = "0.0.3"

[deps]
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
```
12 changes: 4 additions & 8 deletions examples/two_in_one.jl
Original file line number Diff line number Diff line change
@@ -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"])
43 changes: 39 additions & 4 deletions src/ControlPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export plot, plotx, plotxy, plt, load, save
mutable struct PlotX
X
Y
labels
xlabel
ylabels
fig
Expand All @@ -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 != ""
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -88,16 +121,18 @@ 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)
if P.type == 1
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)
Expand Down

0 comments on commit f9f2124

Please sign in to comment.