# Plotting Multiple Bode diagrams

**URL:** <https://discourse.julialang.org/t/plotting-multiple-bode-diagrams/90929>\
**Category:** Modelling & Simulations\
**Tags:** question, plotting, controlsystems\
**Created:** [November 28, 2022, 2:42am UTC](https://discourse.julialang.org/t/plotting-multiple-bode-diagrams/90929 "2022-11-28T02:42:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![emone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emone/32/44130_2.png) [@emone](https://discourse.julialang.org/u/emone)\
**Post date:** [November 28, 2022, 2:42am UTC](https://discourse.julialang.org/t/plotting-multiple-bode-diagrams/90929/1 "2022-11-28T02:42:02Z")

</div>

Bode plots are significant in the design and visualization of control systems. Since I move on Julia for everything, I wonder how to plot multiple responses of bode diagrams, for example, in this code, I got four plots that could be multiple in the same TF on the effect of the Q-factor

f1=bodeplot(LTISystem[G0], label=“G\_0 at Q=0.5”);  
f2=bodeplot(LTISystem[G1], label=“G\_0 at Q=0.6”);  
f3=bodeplot(LTISystem[G2], label=“G\_0 at Q=0.7”);  
f4=bodeplot(LTISystem[G3], label=“G\_0 at Q=0.8”);

I like to obtain a response in 3d, as we can see in this Figure ([Fault-Tolerant Control Based on Sliding Mode Controller for Double-Star Induction Machine | SpringerLink](https://doi.org/10.1007/s13369-019-04120-1))

 ![bode3d](https://global.discourse-cdn.com/julialang/original/3X/a/4/a4ca5de3d703821c4a2ca80c266f7aa0850e23fe.png)

Have any of you already tried?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 28, 2022, 6:02am UTC](https://discourse.julialang.org/t/plotting-multiple-bode-diagrams/90929/2 "2022-11-28T06:02:47Z")

</div>

`ControlSystemsBase` does not have a built-in recipe for producing 3d-plots like that, but here’s something to get you started.

```julia
using ControlSystemsBase, Plots
pyplot()
function meshgrid(a,b)
    grid_a = [i for i in a, j in b]
    grid_b = [j for i in a, j in b]
    grid_a, grid_b
end

ω = 1
G = [tf(ω^2, [1, 2ζ*ω, ω^2]) for ζ in ζs]
ζs = exp10.(LinRange(-4, -0.2, 200))
w = exp10.(LinRange(-2, 2, 200))
Z, W = meshgrid(ζs, w)

M = map(W, Z) do w, ζ
    G = tf(ω^2, [1, 2ζ*ω, ω^2])
    abs(freqresp(G, w)[])
end

surface(log10.(W), Z, log10.(M), xscale=:identity, yscale=:identity, zscale=:identity, xlabel="ω", ylabel="ζ", zlabel="|G(ω)|")

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae85d82c696ab1e34446e1d56bc26b21ababc0cf.jpeg)

---

<div class="post-metadata">

**Author:** ![emone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emone/32/44130_2.png) [@emone](https://discourse.julialang.org/u/emone)\
**Post date:** [November 29, 2022, 10:22pm UTC](https://discourse.julialang.org/t/plotting-multiple-bode-diagrams/90929/3 "2022-11-29T22:22:29Z")

</div>

Hello! It is a beautiful plot, but I couldn’t reproduce your result xD, do you have more information on how can I build it, because I am a newbie, or any site about it?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 30, 2022, 5:34am UTC](https://discourse.julialang.org/t/plotting-multiple-bode-diagrams/90929/4 "2022-11-30T05:34:19Z")

</div>

What problem did you encounter? The code above should be self sufficient, you only need to ensure that the packages that are being loaded at the top are installed.

---

<div class="post-metadata">

**Author:** ![emone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emone/32/44130_2.png) [@emone](https://discourse.julialang.org/u/emone)\
**Post date:** [December 1, 2022, 4:00pm UTC](https://discourse.julialang.org/t/plotting-multiple-bode-diagrams/90929/5 "2022-12-01T16:00:04Z")

</div>

Hello again, I checked the logic and all is fine until I plot the surface, I checked the zeta sweep in every new TF, and again is all good; I only modified the zeta and w sweeps at bottom of G to make it work.

```julia
using ControlSystemsBase, Plots, PyCall
pyplot()

function meshgrid(a,b)
    grid_a = [i for i in a, j in b]
    grid_b = [j for i in a, j in b]
    grid_a, grid_b
end

ω = 1

ζs = exp10.(LinRange(-4, -0.2, 200))
w = exp10.(LinRange(-2, 2, 200))

G = TransferFunction[tf(ω^2, [1, 2ζ*ω, ω^2]) for ζ in ζs]

Z, W = meshgrid(ζs, w)

M = map(W, Z) do w, ζ
    G = tf(ω^2, [1, 2ζ*ω, ω^2])
    abs(freqresp(G, w)[])
end

surface(log10.(W),
        Z,
        log10.(M),
        xscale=:identity,
        yscale=:identity,
        zscale=:identity,
        xlabel="ω",
        ylabel="ζ",
        zlabel="|G(ω)|")

```

The resulting plot is this:

 ![Screenshot from 2022-12-01 09-59-43](https://global.discourse-cdn.com/julialang/original/3X/3/9/39d147abb14fed42ef10ac989fbec159cd31e488.png)

PS. Maybe I made a mistake or something, If you can give me some advice on my problem I will be really grateful

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 1, 2022, 4:07pm UTC](https://discourse.julialang.org/t/plotting-multiple-bode-diagrams/90929/6 "2022-12-01T16:07:25Z")

</div>

Hmm, that exact code gives me the same figure I posted before 🤷‍♂️ Maybe there’s something off with PyPlot on your machine? Can you rotate the figure by clicking with the mouse and dragging?

You could also try another plotting backend, e.g., `gr()` or `plotly()`, here the latter _might_ require you to install `Plotly.jl`

---

<div class="post-metadata">

**Author:** ![emone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emone/32/44130_2.png) [@emone](https://discourse.julialang.org/u/emone)\
**Post date:** [December 1, 2022, 4:34pm UTC](https://discourse.julialang.org/t/plotting-multiple-bode-diagrams/90929/7 "2022-12-01T16:34:49Z")

</div>

Thank you so much! with gr() it works! And you may have the reason that PyPlot doesn’t work as I expected on my machine since I had to run in the terminal, I think is time for a hard reset xD

 ![Screenshot from 2022-12-01 10-32-08](https://global.discourse-cdn.com/julialang/original/3X/4/a/4a1855b6f4cd16e43e35a7fc859f3e74668312b6.png)
