# Having legend on top of twinx() line

**URL:** <https://discourse.julialang.org/t/having-legend-on-top-of-twinx-line/107970>\
**Category:** Visualization\
**Tags:** plotting, plots\
**Created:** [December 23, 2023, 12:24pm UTC](https://discourse.julialang.org/t/having-legend-on-top-of-twinx-line/107970 "2023-12-23T12:24:11Z")\
**Posts on this page:** 2\
**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 23, 2023, 12:24pm UTC](https://discourse.julialang.org/t/having-legend-on-top-of-twinx-line/107970/1 "2023-12-23T12:24:11Z")

</div>

Hi there, I am plotting 4 different sine waves. The scale of one of these waves is on a seperate y axis. Using the code below, I have obtained the following graph:

```julia
plot(sol_2_elem, idxs = wk2model.source.Δp, labels="2 Element WK", ylabel="Pressure", xlims=(7.0,10.0))
plot!(sol_3_elem, idxs = wk3model.source.Δp, labels="3 Element WK", xlims=(7.0,10.0))
plot!(sol_4_elem, idxs = wk4model.source.Δp, labels="4 Element WK", xlims=(7.0,10.0))
plot!(twinx(), t1, i, c=:gray, ls=:dash, xlims=(7.0,10.0), label="")
plot!((0, NaN), c=:gray, ls=:dash, label="Blood Flow (ml/s)")

```

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

Is there a way of having the legend on top of the dashed gray line whose scale is on the second y axis? I tried moving the legend outside of the graph using `legend = :outertopleft`, but that resulted in the following graph, I’m not sure why the gray line didn’t follow the same format.

![image](https://global.discourse-cdn.com/julialang/original/3X/5/0/50a006badf0d7b720da1542a2ea8fc21a36c131a.png)

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 24, 2023, 2:52pm UTC](https://discourse.julialang.org/t/having-legend-on-top-of-twinx-line/107970/2 "2023-12-24T14:52:59Z")

</div>

[[BUG] legend=:outertopleft misalgins x-axes of twinx · Issue #2894 · JuliaPlots/Plots.jl (github.com)](https://github.com/JuliaPlots/Plots.jl/issues/2894)

PS:  
the easiest thing to do might be to increase the `ylims`:

```julia
plot!(legend=:topright, ylims=(-10, 300))

```
