Skip to content

Commit ee9bc1f

Browse files
committed
Better logticks and defaults for bodeplot
1 parent 0ecafbc commit ee9bc1f

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/plotting.jl

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ function setPlotScale(str::AbstractString)
3838
_PlotScale, _PlotScaleFunc, _PlotScaleStr = plotSettings
3939
end
4040

41-
function getLogTicks(x)
41+
function getLogTicks(x, minmax)
42+
minx, maxx = minmax
4243
major_minor_limit = 6
4344
minor_text_limit = 8
44-
min = ceil(log10(minimum(x)))
45-
max = floor(log10(maximum(x)))
45+
min = ceil(log10(minx))
46+
max = floor(log10(maxx))
4647
major = 10.^collect(min:max)
4748
if Plots.backend() != Plots.GRBackend()
4849
majorText = [latexstring("\$10^{$(round(Int64,i))}\$") for i = min:max]
@@ -57,7 +58,7 @@ function getLogTicks(x)
5758
minorText = ["$j*10^{$(round(Int64,i))}" for i = (min-1):(max+1) for j = 2:9]
5859
end
5960

60-
ind = find(minimum(x) .<= minor .<= maximum(x))
61+
ind = find(minx .<= minor .<= maxx)
6162
minor = minor[ind]
6263
minorText = minorText[ind]
6364
if length(minor) > minor_text_limit
@@ -205,7 +206,7 @@ optionally provided.
205206
`kwargs` is sent as argument to Plots.plot.""" ->
206207
bodeplot
207208

208-
@recipe function bodeplot(p::Bodeplot; plotphase=true)
209+
@recipe function bodeplot(p::Bodeplot; plotphase=true, ylimsphase=nothing)
209210
systems = p.args[1]
210211
if !isa(systems, AbstractArray)
211212
systems = [systems]
@@ -222,7 +223,7 @@ bodeplot
222223
s2i(i,j) = sub2ind((nu,(plotphase?2:1)*ny),j,i)
223224
layout := ((plotphase?2:1)*ny,nu)
224225
nw = length(w)
225-
xticks --> getLogTicks(w)
226+
xticks --> getLogTicks(w, get(plotattributes, :xlims, (minimum(w), maximum(w))))
226227

227228
for (si,s) = enumerate(systems)
228229
mag, phase = bode(s, w)[1:2]
@@ -246,7 +247,7 @@ bodeplot
246247
yscale --> _PlotScaleFunc
247248
xscale --> :log10
248249
if _PlotScale != "dB"
249-
yticks --> getLogTicks(magdata)
250+
yticks --> getLogTicks(magdata, get(plotattributes, :ylims, (minimum(magdata), maximum(magdata))))
250251
end
251252
xguide --> xlab
252253
yguide --> "Magnitude $_PlotScaleStr"
@@ -260,9 +261,17 @@ bodeplot
260261
if !plotphase
261262
continue
262263
end
264+
#Let user specify limits on phase independently
265+
ylimsphase = if isa(ylimsphase, Void)
266+
(floor(minimum(phasedata)/180)*180,
267+
ceil(maximum(phasedata)/180)*180)
268+
else
269+
ylimsphase
270+
end
263271
@series begin
264272
grid --> true
265273
xscale --> :log10
274+
ylims := ylimsphase
266275
yguide --> "Phase (deg)"
267276
subplot := s2i(2i,j)
268277
xguide --> "Frequency (rad/s)"
@@ -287,8 +296,8 @@ end
287296
yscale --> :log10
288297
xscale --> :log10
289298
yguide --> "Magnitude"
290-
xticks --> getLogTicks(w)
291-
yticks --> getLogTicks(magdata)
299+
xticks --> getLogTicks(w, get(plotattributes, :xlims, (minimum(w), maximum(w))))
300+
yticks --> getLogTicks(magdata, get(plotattributes, :ylims, (minimum(magdata), maximum(magdata))))
292301
x := w; y := magdata
293302
()
294303
end
@@ -306,7 +315,7 @@ end
306315
xscale --> :log10
307316
yguide --> "Phase (deg)"
308317
xguide --> "Frequency (rad/s)"
309-
xticks --> getLogTicks(w)
318+
xticks --> getLogTicks(w, get(plotattributes, :xlims, (minimum(w), maximum(w))))
310319
x := w; y := phasedata
311320
()
312321
end

0 commit comments

Comments
 (0)