# Lining up zeros on plot with 2 y axes

**URL:** <https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736>\
**Category:** Visualization\
**Tags:** diffeq, plots, recipes\
**Created:** [December 17, 2023, 4:36pm UTC](https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736 "2023-12-17T16:36:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aoifehealy](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@aoifehealy](https://discourse.julialang.org/u/aoifehealy)\
**Post date:** [December 17, 2023, 4:36pm UTC](https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736/1 "2023-12-17T16:36:56Z")

</div>

Hi there,

I am plotting 2 different datasets on the same plot, but using different scaled y axes, using the following code:

```julia
plot(ShiSimpleSolV, idxs = [circ_sys.MV.q])

plot!(twinx(), ShiSimpleSolV, idxs = [circ_sys.MV.Δp], linecolor=:red)

```

which produces:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/0/90f328f6c1decdf27089d614ab016ce015ef0da0.png)

I was wondering if there was a way to line up the zeros so they are at the same height? I want to try to show how the blue line only increases when the red line is below zero.

Also is there a way to turn off the second x axis label?

Thank you!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 17, 2023, 10:09pm UTC](https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736/3 "2023-12-17T22:09:40Z")

</div>

Since you seem to be using the DifferentialEquations.jl Plots’ recipes, you could try the example below (where `sol` is the diffeq solution):

```julia
using Plots; gr(dpi=600)

# Assumes that 1st curve has both + and - values
a, b, c, d = extrema(sol[1,:])..., extrema(sol[2,:])...
α = -b/a
c * d ≥ 0 && (c = -d/α)
β = -d/c
α > β ? d = -α*c : c = -d/α

plot(sol, idxs=1, ylims=(a,b), c=:blue, framestyle=:zerolines, ylabel="u1", yguidefontcolor=:blue, legend=:topright)

plot!(twinx(), sol, idxs=2, ylims=(c,d), c=:red, grid=true, xlabel="", ylabel="u2", yguidefontcolor=:red, legend=:bottomleft)

```

Plot example:

 ![Plots_twinx_align_origin](https://global.discourse-cdn.com/julialang/original/3X/9/0/90b4f8bef7139e80068e19799a5118c0c5070de4.png)

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 17, 2023, 10:14pm UTC](https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736/4 "2023-12-17T22:14:34Z")

</div>

With the same concept as @rafael.guerra 's answer. But a much more verbose version:

```julia
function matchpratio(lims1, lims2, p = 0.0)
    lo, hi = lims1
    lo >= hi && error("lims must be ordered")
    newlo = min(p, lo)
    newhi = max(p, hi)
    pratio = (p - newlo) / (newhi - newlo)
    lo2, hi2 = lims2
    lo2 >= hi2 && error("lims must be ordered")
    newlo2 = min(p, lo2)
    newhi2 = max(p, hi2)
    pratio2 = (p - newlo2) / (newhi2 - newlo2)
    if pratio2 > pratio
        newhi2 = (p - newlo2) / pratio
    else
        newlo2 = newhi2 - (newhi2 - p) / (1 - pratio)
    end
    if any(isinf, (newlo, newhi, newlo2, newhi2))
        a,b = matchpratio(lims2, lims1, p)
        return (b,a)
    end
    return ((newlo, newhi), (newlo2, newhi2))
end

```

and now:

```julia
julia> y1 = cumsum(rand(10));

julia> y2 = cumsum(rand(10)) .- 3;

julia> l1, l2 = matchpratio(extrema(y1), extrema(y2))
((-5.927414300344668, 6.473046090935813), (-2.743430915758225, 2.9959698892599125))

julia> plt = plot(y1; line = (:green, :step), ylim=l1);

julia> plot!(twinx(), y2; line = (:red, :step), ylim=l2)

```

and the function `matchpratio` works for other points of interest using the third argument.

---

<div class="post-metadata">

**Author:** ![aoifehealy](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@aoifehealy](https://discourse.julialang.org/u/aoifehealy)\
**Post date:** [December 18, 2023, 1:52pm UTC](https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736/5 "2023-12-18T13:52:37Z")

</div>

That’s great thank you so much, it now looks like this:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/3/930353b704a7687bd34b84eb255e875e497cdee8.png)  
I was wondering if there was a way to add in the markers for the u1 y axis while still having the x axis at the zero line?  
I got rid of the `framestyle=:zerolines` in the first plot(), which gave this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/4/048f3d2f6eee56d37a99610b0e0ffe6a5c0f7645.png)  
Which is still fine but ideally the black x-axis line would still be at the zero line.

Also, is there a way of having the legends in the same box?

Thanks again?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 18, 2023, 2:04pm UTC](https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736/6 "2023-12-18T14:04:47Z")

</div>

> [@aoifehealy](#):
>
> I was wondering if there was a way to add in the markers for the u1 y axis while still having the x axis at the zero line?

I do not understand this requirement, could you ellaborate?

> [@aoifehealy](#):
>
> Also, is there a way of having the legends in the same box?

Yes, you can check the edited code:

```julia
plot(sol, idxs=1, ylims=(a,b), c=:blue, framestyle=:zerolines, ylabel="u1", yguidefontcolor=:blue, legend=:topright)
plot!((0, NaN), c=:red, label="u2(t)")
plot!(twinx(), sol, idxs=2, ylims=(c,d), c=:red, grid=true, xlabel="", ylabel="u2", yguidefontcolor=:red, label="")

```

which plots:

 ![Plots_twinx_align_origin](https://global.discourse-cdn.com/julialang/original/3X/f/d/fd28f3a241af57fe0411db0e42c7c703edd4362c.png)

---

<div class="post-metadata">

**Author:** ![aoifehealy](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@aoifehealy](https://discourse.julialang.org/u/aoifehealy)\
**Post date:** [December 18, 2023, 2:36pm UTC](https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736/7 "2023-12-18T14:36:31Z")

</div>

> [@rafael.guerra](#):
>
> I do not understand this requirement, could you ellaborate?

Hi sorry, hopefully I can explain a bit better. When I run the following code (your example applied to my data)

```julia
a, b, c, d = extrema(sol[circ_sys.MV.Δp])..., extrema(sol[circ_sys.MV.q])...
α = -b/a
c * d ≥ 0 && (c = -d/α)
β = -d/c
α > β ? d = -α*c : c = -d/α

plot(sol, idxs=circ_sys.MV.Δp, ylims=(a,b), c=:blue, framestyle=:zerolines, ylabel="u1", yguidefontcolor=:blue, legend=:topright)

plot!(twinx(), sol, idxs=circ_sys.MV.q, ylims=(c,d), c=:red, grid=true, xlabel="", ylabel="u2", yguidefontcolor=:red, legend=:bottomleft)

```

I get the following graph

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

Here the black line for the x-axis is where I want it to be (at y=0), but I would like to add a black line at the u1 y-axis. In order to do this, I tried to get rid of the `framestyle=:zerolines` parameter in the first plot() command, resulting in the following plot commands:

```julia
plot(sol, idxs=circ_sys.MV.Δp, ylims=(a,b), c=:blue, ylabel="u1", yguidefontcolor=:blue, legend=:topright)

plot!(twinx(), sol, idxs=circ_sys.MV.q, ylims=(c,d), c=:red, grid=true, xlabel="", ylabel="u2", yguidefontcolor=:red, legend=:bottomleft)

```

And gives the following graph:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/6/f6d38b59ac0958d12494bf7756e515582da57801.png)

This has the black line at the u1 y-axis that I want, however the the black x-axis is no longer at the y=0 line.

Is there a way of resolving this? I see that in the plot you have as an example, this black y-axis line is there, so I don’t know why this is not working for me.

Thanks again

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 18, 2023, 2:50pm UTC](https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736/8 "2023-12-18T14:50:32Z")

</div>

Ah, OK, that is because my plot starts at t=0.

A simple fix is to add a vertical line at the initial time `t₀` of your simulation.

```julia
vline!([t₀], c=:black)

```

With more labor, you could add the `xticks` too.

---

<div class="post-metadata">

**Author:** ![aoifehealy](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@aoifehealy](https://discourse.julialang.org/u/aoifehealy)\
**Post date:** [December 21, 2023, 11:28am UTC](https://discourse.julialang.org/t/lining-up-zeros-on-plot-with-2-y-axes/107736/9 "2023-12-21T11:28:26Z")

</div>

Thanks so much for all your help!
