# Customizing legends of rootlocusplot by ControlSystem.jl?

**URL:** https://discourse.julialang.org/t/customizing-legends-of-rootlocusplot-by-controlsystem-jl/89756
**Category:** Modelling & Simulations
**Tags:** question, plotting, controlsystems
**Created:** [November 4, 2022, 3:22am UTC](https://discourse.julialang.org/t/customizing-legends-of-rootlocusplot-by-controlsystem-jl/89756 "2022-11-04T03:22:45Z")
**Posts on this page:** 4
**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 4, 2022, 3:22am UTC](https://discourse.julialang.org/t/customizing-legends-of-rootlocusplot-by-controlsystem-jl/89756/1 "2022-11-04T03:22:45Z")

</div>

Hello mates, I would like to manipulate the rootlocusplot syntax by modifying the legends to be able to differentiate the system that I am graphing. Has anyone tried this?

My algorithm is this

using ControlSystems  
using Plots

Gid = tf(numI,den);  
Gvd = tf(numV,den);  
rlocusplot(Gvd,xlims=(-4e4,4e4),ylims=(-2.5e4,2.5e4),) # ← ,label=‘Gvd’ ?  
rlocusplot!(Gid,xlims=(-4e4,4e4),ylims=(-2.5e4,2.5e4),) # ← ,label=‘Gid’ ?

 ![Screenshot from 2022-11-03 21-21-05](https://global.discourse-cdn.com/julialang/original/3X/4/7/47f1f02be00716beef7bd320f36e9bb30fe8823c.png)

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [November 4, 2022, 8:06am UTC](https://discourse.julialang.org/t/customizing-legends-of-rootlocusplot-by-controlsystem-jl/89756/2 "2022-11-04T08:06:25Z")

</div>

It seems like that is not implemented, though doesn’t seem like it would be a problem to add.

In the meantime you could call `rlocus` to get the roots and zeros and then plot them yourself, though this was only added in ControlSystems v1.5.1 so you have to make sure you have the latest version.

I haven’t tested the code, but I think something like this should work.

```julia
roots1, Z1, K1 = rlocus(Gid; K)
roots2, Z2, K2 = rlocus(Gvd; K)

plot(real.(roots1), imag.(roots1), label="Gid poles")
plot!(real.(Z1), imag.(Z1), label="Gid zeros")
plot!(real.(roots2), imag.(roots2), label="Gvd poles")
plot!(real.(Z2), imag.(Z2), label="Gvd zeros")

```

---

<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 4, 2022, 9:04pm UTC](https://discourse.julialang.org/t/customizing-legends-of-rootlocusplot-by-controlsystem-jl/89756/3 "2022-11-04T21:04:25Z")

</div>

Thanks for reply albheim, I tried to include the function rlocus but my problem now is I don’t know what ‘K’ must be in the algorithm, the thing that I did was use the module Polynomials.jl and simple plot the roots. In the future if you test the rlocus function, it will be awesome to share…

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [November 5, 2022, 8:19am UTC](https://discourse.julialang.org/t/customizing-legends-of-rootlocusplot-by-controlsystem-jl/89756/4 "2022-11-05T08:19:56Z")

</div>

If you are in the REPL you can go into help mode by entering a question mark on an empty line, and then write the name of a function to get the docstrings for it.

```julia
help?> rlocus
search: rlocus rlocusplot rlocusplot!

  roots, Z, K = rlocus(P::LTISystem; K)

  Compute the root locus of the SISO LTISystem P with a negative feedback loop and feedback gains
  between 0 and K. rlocus will use an adaptive step-size algorithm to determine the values of the
  feedback gains used to generate the plot.

  roots is a complex matrix containig the poles trajectories of the closed-loop 1+k⋅G(s) as a
  function of k, Z contains the zeros of the open-loop system G(s) and K the values of the
  feedback gain.

```

So here you see that we generate the closed loop poles and zeros for feedback gains between 0 and K. Though, something we don’t see here but can find in the code is that you can leave K out from the call and then it will default to 500.

I also realised you would get some double labels with what I said before, so here is a better recreation of the plot. It doesn’t take care of the colors well, but I’m sure you can fix that if you want.

```julia
roots1, Z1, K1 = rlocus(Gid)
roots2, Z2, K2 = rlocus(Gvd)

plot(real.(roots1), imag.(roots1), label=false)
scatter!(real.(roots1)[1,:], imag.(roots1)[1,:], markershape=:xcross, label="Gid poles")
scatter!(real.(Z1), imag.(Z1), markershape=:circle, label="Gid zeros")
plot!(real.(roots2), imag.(roots2), label=false)
scatter!(real.(roots2)[1,:], imag.(roots2)[1,:], markershape=:xcross, label="Gvd poles")
scatter!(real.(Z2), imag.(Z2), markershape=:circle, label="Gvd zeros")

```
