# Plotting parameters and variables on the same plot for Differential Equation solution

**URL:** <https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745>\
**Category:** General Usage\
**Tags:** plotting, plots, differentialequation\
**Created:** [May 12, 2023, 2:39pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745 "2023-05-12T14:39:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![GertEhlers27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gertehlers27/32/49281_2.png) [@GertEhlers27](https://discourse.julialang.org/u/GertEhlers27)\
**Post date:** [May 12, 2023, 2:39pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/1 "2023-05-12T14:39:59Z")

</div>

Hi Everyone,  
I am using DifferentialEquations.jl to solve an ODE. After solving it, I plot the different variables.  
The problem is:  
I want to add a parameter (p) to the same plot. Is it possible? I only see different ways of plotting the variables, but no way to add parameters to the plot.  
The reason I want to see the parameter, is because it is changed a few times using iterative callbacks (DiffEqCallbacks).

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 12, 2023, 2:54pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/2 "2023-05-12T14:54:28Z")

</div>

Could you explain what you mean by “see the parameter”? How would it appear on the plot - is it a point on the plot, a line, some text somewhere?

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [May 12, 2023, 3:11pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/3 "2023-05-12T15:11:02Z")

</div>

Do you want to display the parameter as a text ( annotation) on your plot? Are you plotting a solution coordinates, t\to x\_i(t), for each i, or the trajectory t\to (x\_1(t),\ldots ,x\_n(t)), n=2 or 3?  
A minimal example and a more precise formulation of your intention to display the parameter for some interval of time, t, would be useful.

---

<div class="post-metadata">

**Author:** ![GertEhlers27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gertehlers27/32/49281_2.png) [@GertEhlers27](https://discourse.julialang.org/u/GertEhlers27)\
**Post date:** [May 12, 2023, 3:14pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/4 "2023-05-12T15:14:58Z")

</div>

My plot is two variable plotted over time (which is the solution of the differential equation). While the parameter is a constant value given as an input to the differential equation.  
The parameter would be a line on the plot. It would have been a straight horizontal line, but since the callback function changes the value at specified times, it should look like steps when plotted over time.

Here is my code, if it might help:

```julia
using Pkg, ModelingToolkit, BlockSystems, OrdinaryDiffEq, Plots, Interpolations, Symbolics

function interp(t) 
    time = [0.05, 1.0, 2.0, 3.0, 6.0, 7.0, 8.0]
    P_elec = [1.0, 1.0, 0.6, 1.0, 1.0, 1.6, 1.6]
    interpoleer = linear_interpolation(time, P_elec, extrapolation_bc=Line())
    return interpoleer(t)
end

@parameters t M D P_m(t) P_e(t)
@variables ω(t)

dt = Differential(t)
swing = IOBlock([dt(ω) ~ 1/M * (P_m - D*ω - P_e)], # equations
                 [P_m], # inputs
                 [ω]) # outputs

swing = set_p(swing, :D=>1, :M=>1)

@variables P_m(t)
pfix = IOBlock([P_m ~ 1], [], [P_m])
swing_with_P_m_P_e = IOSystem([pfix.P_m => swing.P_m], [pfix, swing]) |> connect_system

gen = generate_io_function(swing_with_P_m_P_e)

u0 = zeros(length(gen.states))
odef = ODEFunction((du, u, p, t) -> gen.f_ip(du, u, nothing, p, t); mass_matrix=gen.massm, syms=Symbol.(gen.states))

tspan = (0.0, 10.0)
p = [0.99] #P_e as parameter
prob = ODEProblem(odef, u0, tspan, p)

time_choice(integrator) = 0.05 + integrator.t
affect!(integrator) = integrator.p .= [interp(integrator.t)] 
cb = IterativeCallback(time_choice, affect!)
sol = solve(prob, Rodas4(autodiff=false), callback = cb) 

size = (500, 300)
linewidth=2
palette=:tab10
grid=false
xlabel = "Time"

plot(sol; linewidth, legend=:right, palette, grid, xlabel, ylabel="Frequency / Power", size, title="fixed P_m")

```

---

<div class="post-metadata">

**Author:** ![GertEhlers27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gertehlers27/32/49281_2.png) [@GertEhlers27](https://discourse.julialang.org/u/GertEhlers27)\
**Post date:** [May 12, 2023, 3:17pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/5 "2023-05-12T15:17:49Z")

</div>

@empet, I added my code to try better explain this in a reply to @nilshg . Hope it helps?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 12, 2023, 3:19pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/6 "2023-05-12T15:19:50Z")

</div>

> [@GertEhlers27](#):
>
> It would have been a straight horizontal line, but since the callback function changes the value at specified times, it should look like steps when plotted over time.

A horizontal line is:

```julia
hline!([parameter_value], label = "parameter")

```

but if you want `P_elec` at different points in time it’s:

```julia
plot!(time, P_elec, label = "parameter")

```

In general the differential equations ecosystem uses Plots.jl recipes so all the plots once created can just be used like any other Plots.jl plot, i.e. they can be modified by the relevant commands with a bang `!` added.

---

<div class="post-metadata">

**Author:** ![GertEhlers27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gertehlers27/32/49281_2.png) [@GertEhlers27](https://discourse.julialang.org/u/GertEhlers27)\
**Post date:** [May 13, 2023, 12:00pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/7 "2023-05-13T12:00:45Z")

</div>

Thanks.

Though it didn’t really solve the problem. I am trying to plot “p” over “t”, not “P\_elec” over “time”.  
The code: `affect!(integrator) = integrator.p .= [interp(integrator.t)]`, calls values of P\_elec at different times, but plotting P\_elec over Time will not give me the same as “p” over “t”.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 13, 2023, 12:23pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/8 "2023-05-13T12:23:43Z")

</div>

You’d have to save it yourself. `p` isn’t logged.

---

<div class="post-metadata">

**Author:** ![GertEhlers27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gertehlers27/32/49281_2.png) [@GertEhlers27](https://discourse.julialang.org/u/GertEhlers27)\
**Post date:** [May 13, 2023, 12:38pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/9 "2023-05-13T12:38:33Z")

</div>

Thanks @ChrisRackauckas.  
I’ll go and try that. Do you have any references/links where I can learn more about logging parameters?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 13, 2023, 3:52pm UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/10 "2023-05-13T15:52:00Z")

</div>

> [@GertEhlers27](#):
>
> Do you have any references/links where I can learn more about logging parameters?

Just add a push in the callback

---

<div class="post-metadata">

**Author:** ![GertEhlers27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gertehlers27/32/49281_2.png) [@GertEhlers27](https://discourse.julialang.org/u/GertEhlers27)\
**Post date:** [May 16, 2023, 8:10am UTC](https://discourse.julialang.org/t/plotting-parameters-and-variables-on-the-same-plot-for-differential-equation-solution/98745/11 "2023-05-16T08:10:17Z")

</div>

Thanks. It works!

For others that might want to see the push!():

```julia
function simulate(sys)
    gen = generate_io_function(sys)

    u0 = zeros(length(gen.states))
    odef = ODEFunction((du, u, p, t) -> gen.f_ip(du, u, nothing, p, t); mass_matrix=gen.massm, syms=Symbol.(gen.states))

    #@info "Gen state" gen.states
    tspan = (0, 5)
    p = [0.8] #P_e as parameter >>>Is this only P_e, or is this a combination of the D, M, and P_e parameters?
    #@info p
    prob = ODEProblem(odef, u0, tspan, p)

    
    # Logging callback
    logged_p = []
    logged_t = []
    time_choice(integrator) = 0.001 + integrator.t
    affect!(integrator) = begin
        integrator.p .= [interp(integrator.t)]
        push!(logged_p, integrator.p[1])
        push!(logged_t, integrator.t)
    end
    cb = IterativeCallback(time_choice, affect!)

    sol = solve(prob, Rodas4(autodiff=false), callback=cb)
    return sol, logged_p, logged_t
end

```
