Skip to content

Commit fb94838

Browse files
authored
Merge pull request #71 from JuliaControl/newplotssyntax
New plots syntax for Plots 0.7
2 parents 37bf6f5 + a49a098 commit fb94838

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
julia 0.4
2-
Plots
2+
Plots v0.7.4
33
Polynomials

src/plotting.jl

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ function lsimplot{T<:StateSpace}(systems::Vector{T}, u::Union{AbstractVecOrMat,F
5555
error("All systems must have the same input/output dimensions")
5656
end
5757
ny, nu = size(systems[1])
58-
fig = Plots.subplot(n=ny, nr=ny)
58+
fig = Plots.plot(layout=(ny,1))
59+
s2i(i,j) = sub2ind((ny,1),j,i)
5960
for (si, s) in enumerate(systems)
6061
y = lsim(s, u, t, x0, method)[1]
6162
for i=1:ny
6263
ydata = reshape(y[:, i], size(t, 1))
6364
style = iscontinuous(s) ? :path : :steppost
6465
ytext = (ny > 1) ? "Amplitude to: y($i)": "Amplitude"
65-
Plots.plot!(fig[i,1], t, ydata, l=style, xlabel="Time (s)", ylabel=ytext, title="System Response", lab="\$G_\{$(si)\}\$"; getStyleSys(si,length(systems))..., kwargs...)
66+
Plots.plot!(fig, t, ydata, l=style, xlabel="Time (s)", ylabel=ytext, title="System Response", subplot=s2i(1,i), lab="\$G_\{$(si)\}\$"; getStyleSys(si,length(systems))..., kwargs...)
6667
end
6768
end
6869
return fig
@@ -80,7 +81,9 @@ for (func, title) = ((:step, "Step Response"), (:impulse, "Impulse Response"))
8081
error("All systems must have the same input/output dimensions")
8182
end
8283
ny, nu = size(systems[1])
83-
fig = Plots.subplot(n = ny*nu, nr = ny)
84+
fig = Plots.plot(layout=(ny,nu))
85+
titles = fill("", ny*nu)
86+
s2i(i,j) = sub2ind((ny,nu),j,i)
8487
for (si,(s, Ts)) in enumerate(zip(systems, Ts_list))
8588
t = 0:Ts:Tf
8689
y = ($func)(s, t)[1]
@@ -89,11 +92,13 @@ for (func, title) = ((:step, "Step Response"), (:impulse, "Impulse Response"))
8992
ydata = reshape(y[:, i, j], size(t, 1))
9093
style = iscontinuous(s) ? :path : :steppost
9194
ttext = (nu > 1 && i==1) ? $title*" from: u($j) " : $title
95+
titles[s2i(i,j)] = ttext
9296
ytext = (ny > 1 && j==1) ? "Amplitude to: y($i)": "Amplitude"
93-
Plots.plot!(fig[i,j], t, ydata, l=style, xlabel="Time (s)", ylabel=ytext, title=ttext, lab="\$G_\{$(si)\}\$"; getStyleSys(si,length(systems))..., kwargs...)
97+
Plots.plot!(fig, t, ydata, l=style, xlabel="Time (s)", ylabel=ytext, subplot=s2i(i,j), lab="\$G_\{$(si)\}\$"; getStyleSys(si,length(systems))..., kwargs...)
9498
end
9599
end
96100
end
101+
Plots.plot!(fig, title=titles')
97102
return fig
98103
end
99104
$funcname{T<:LTISystem}(systems::Vector{T}, Tf::Real; kwargs...) =
@@ -129,7 +134,8 @@ function bodeplot{T<:LTISystem}(systems::Vector{T}, w::AbstractVector; plotphase
129134
error("All systems must have the same input/output dimensions")
130135
end
131136
ny, nu = size(systems[1])
132-
fig = Plots.subplot(n=(plotphase?2:1)*ny*nu, nc=nu)
137+
fig = Plots.plot(layout=((plotphase?2:1)*ny,nu))
138+
s2i(i,j) = sub2ind((nu,(plotphase?2:1)*ny),j,i)
133139
nw = length(w)
134140
for (si,s) = enumerate(systems)
135141
mag, phase = bode(s, w)[1:2]
@@ -145,8 +151,8 @@ function bodeplot{T<:LTISystem}(systems::Vector{T}, w::AbstractVector; plotphase
145151
continue
146152
end
147153
phasedata = vec(phase[:, i, j])
148-
Plots.plot!(fig[(plotphase?(2i-1):i),j], w, magdata, grid=true, yscale=_PlotScaleFunc, xscale=:log10, xlabel=xlab, title="Bode plot from: u($j)", ylabel="Magnitude $_PlotScaleStr", lab="\$G_\{$(si)\}\$"; getStyleSys(si,length(systems))..., kwargs...)
149-
plotphase && Plots.plot!(fig[2i,j], w, phasedata, grid=true, xscale=:log10, ylabel="Phase (deg)",xlabel="Frequency (rad/s)"; getStyleSys(si,length(systems))..., kwargs...)
154+
Plots.plot!(fig, w, magdata, grid=true, yscale=_PlotScaleFunc, xscale=:log10, xlabel=xlab, subplot=s2i((plotphase?(2i-1):i),j), title="Bode plot from: u($j)", ylabel="Magnitude $_PlotScaleStr", lab="\$G_\{$(si)\}\$"; getStyleSys(si,length(systems))..., kwargs...)
155+
plotphase && Plots.plot!(fig, w, phasedata, grid=true, xscale=:log10, ylabel="Phase (deg)", subplot=s2i(2i,j), xlabel="Frequency (rad/s)", lab="\$G_\{$(si)\}\$"; getStyleSys(si,length(systems))..., kwargs...)
150156
end
151157
end
152158
end
@@ -169,7 +175,8 @@ function nyquistplot{T<:LTISystem}(systems::Vector{T}, w::AbstractVector; neg=fa
169175
end
170176
ny, nu = size(systems[1])
171177
nw = length(w)
172-
fig = Plots.subplot(n=ny*nu, nc= nu)
178+
fig = Plots.plot(layout=(ny,nu))
179+
s2i(i,j) = sub2ind((ny,nu),j,i)
173180
# Ensure that `axes` is always a matrix of handles
174181
for (si,s) = enumerate(systems)
175182
re_resp, im_resp = nyquist(s, w)[1:2]
@@ -179,13 +186,13 @@ function nyquistplot{T<:LTISystem}(systems::Vector{T}, w::AbstractVector; neg=fa
179186
imdata = im_resp[:, i, j]
180187
ylim = (min(max(-20,minimum(imdata)),-1), max(min(20,maximum(imdata)),1))
181188
xlim = (min(max(-20,minimum(redata)),-1), max(min(20,maximum(redata)),1))
182-
Plots.plot!(fig[i, j],redata, imdata, title="Nyquist plot from: u($j)", ylabel="To: y($i)", ylims=ylim, xlims=xlim; getStyleSys(si,length(systems))..., kwargs...)
189+
Plots.plot!(fig,redata, imdata, title="Nyquist plot from: u($j)", ylabel="To: y($i)", ylims=ylim, xlims=xlim, subplot=s2i(i,j), lab="\$G_\{$(si)\}\$"; getStyleSys(si,length(systems))..., kwargs...)
183190

184191
if si == length(systems)
185192
v = linspace(0,2π,100)
186193
S,C = sin(v),cos(v)
187-
Plots.plot!(fig[i, j],C,S,l=:dash,c=:black, lab="")
188-
Plots.plot!(fig[i, j],C-1,S,l=:dash,c=:red, grid=true, lab="")
194+
Plots.plot!(fig,C,S,l=:dash,c=:black, lab="", subplot=s2i(i,j))
195+
Plots.plot!(fig,C-1,S,l=:dash,c=:red, grid=true, lab="", subplot=s2i(i,j))
189196
# neg && Plots.plot!(fig[i, j],redata, -imdata, args...)
190197
end
191198
end
@@ -381,7 +388,7 @@ function marginplot{T<:LTISystem}(systems::Vector{T}, w::AbstractVector; kwargs.
381388
end
382389
ny, nu = size(systems[1])
383390
fig = bodeplot(systems, w, kwargs...)
384-
391+
s2i(i,j) = sub2ind((ny,nu),j,i)
385392
titles = Array(AbstractString,nu,ny,2,2)
386393
titles[:,:,1,1] = "Gm: "
387394
titles[:,:,2,1] = "Pm: "
@@ -400,17 +407,17 @@ function marginplot{T<:LTISystem}(systems::Vector{T}, w::AbstractVector; kwargs.
400407
end
401408
for k=1:length(wgm)
402409
#Plot gain margins
403-
Plots.plot!(fig[2i-1,j], [wgm[k];wgm[k]], [1;mag[k]], lab=""; getStyleSys(si,length(systems))...)
410+
Plots.plot!(fig, [wgm[k];wgm[k]], [1;mag[k]], lab="", subplot=s2i(2i-1,j); getStyleSys(si,length(systems))...)
404411
end
405412
#Plot gain line at 1
406-
Plots.plot!(fig[2i-1,j], [w[1],w[end]], [oneLine,oneLine], l=:dash, c=:gray, lab="")
413+
Plots.plot!(fig, [w[1],w[end]], [oneLine,oneLine], l=:dash, c=:gray, lab="", subplot=s2i(2i-1,j))
407414
titles[j,i,1,1] *= "["*join([@sprintf("%2.2f",v) for v in gm],", ")*"] "
408415
titles[j,i,1,2] *= "["*join([@sprintf("%2.2f",v) for v in wgm],", ")*"] "
409416
for k=1:length(wpm)
410417
#Plot the phase margins
411-
Plots.plot!(fig[2i,j], [wpm[k];wpm[k]],[fullPhase[k];fullPhase[k]-pm[k]], lab=""; getStyleSys(si,length(systems))...)
418+
Plots.plot!(fig, [wpm[k];wpm[k]],[fullPhase[k];fullPhase[k]-pm[k]], lab="", subplot=s2i(2i,j); getStyleSys(si,length(systems))...)
412419
#Plot the line at 360*k
413-
Plots.plot!(fig[2i,j], [w[1],w[end]],(fullPhase[k]-pm[k])*ones(2), l=:dash, c=:gray, lab="")
420+
Plots.plot!(fig, [w[1],w[end]],(fullPhase[k]-pm[k])*ones(2), l=:dash, c=:gray, lab="", subplot=s2i(2i,j))
414421
end
415422
titles[j,i,2,1] *= "["*join([@sprintf("%2.2f",v) for v in pm],", ")*"] "
416423
titles[j,i,2,2] *= "["*join([@sprintf("%2.2f",v) for v in wpm],", ")*"] "
@@ -419,8 +426,8 @@ function marginplot{T<:LTISystem}(systems::Vector{T}, w::AbstractVector; kwargs.
419426
end
420427
for j = 1:nu
421428
for i = 1:ny
422-
Plots.title!(fig[2i-1,j], titles[j,i,1,1]*" "*titles[j,i,1,2])
423-
Plots.title!(fig[2i,j], titles[j,i,2,1]*" "*titles[j,i,2,2])
429+
Plots.title!(fig, titles[j,i,1,1]*" "*titles[j,i,1,2], subplot=s2i(2i-1,j))
430+
Plots.title!(fig, titles[j,i,2,1]*" "*titles[j,i,2,2], subplot=s2i(2i,j))
424431
end
425432
end
426433
return fig
@@ -482,10 +489,11 @@ function gangoffourplot(P::Union{Vector, LTISystem}, C::Vector, args...; plotpha
482489
S,D,N,T = gangoffour(P,C)
483490
fig = bodeplot(LTISystem[[S[i] D[i]; N[i] T[i]] for i = 1:length(C)], args..., plotphase=plotphase; kwargs...)
484491
lower = plotphase ? 3 : 2
485-
Plots.plot!(fig[1,1],title="\$S = 1/(1+PC)\$")
486-
Plots.plot!(fig[1,2],title="\$D = P/(1+PC)\$")
487-
Plots.plot!(fig[lower,1],title="\$N = C/(1+PC)\$")
488-
Plots.plot!(fig[lower,2],title="\$T = PC/(1+PC\$)")
492+
s2i(i,j) = sub2ind((2,(plotphase?4:2)),j,i)
493+
Plots.plot!(fig,title="\$S = 1/(1+PC)\$", subplot=s2i(1,1))
494+
Plots.plot!(fig,title="\$D = P/(1+PC)\$", subplot=s2i(1,2))
495+
Plots.plot!(fig,title="\$N = C/(1+PC)\$", subplot=s2i(lower,1))
496+
Plots.plot!(fig,title="\$T = PC/(1+PC\$)", subplot=s2i(lower,2))
489497
return fig
490498
end
491499

0 commit comments

Comments
 (0)