# Dual-axis plot breaks with multiple variables on right/secondary y-axis

**URL:** <https://discourse.julialang.org/t/dual-axis-plot-breaks-with-multiple-variables-on-right-secondary-y-axis/64491>\
**Category:** General Usage\
**Tags:** question, plotting, visualization, plots\
**Created:** [July 12, 2021, 8:24am UTC](https://discourse.julialang.org/t/dual-axis-plot-breaks-with-multiple-variables-on-right-secondary-y-axis/64491 "2021-07-12T08:24:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tai/32/5799_2.png) [@tai](https://discourse.julialang.org/u/tai)\
**Post date:** [July 12, 2021, 8:24am UTC](https://discourse.julialang.org/t/dual-axis-plot-breaks-with-multiple-variables-on-right-secondary-y-axis/64491/1 "2021-07-12T08:24:43Z")

</div>

I’m facing a garbled plot when I do a dual-axis plot with multiple variables on right/secondary y-axis.  
Here, I’m trying to do multiple lineplots on top of a barplot.

```julia
using Plots
using Plots.PlotMeasures
pyplot()
bar(rand(10), c=:white, right_margin=1cm)
plot!(twinx(), rand(10), label="v1")
plot!(twinx(), rand(10) * 10, label="v2")

```

![image](https://global.discourse-cdn.com/julialang/original/3X/0/1/014d510ffb89e532bacffd5dc83f396b1168c077.png)

I see following issues:

1. Line plots for both “v1” and “v2” are in the same color
2. Legend of “v1” is not drawn (I presume it’s hidden by “v2” legend)
3. Ticks on right y-axis are drawn twice, with different y-range

If I swap left/right y-axis (lineplots on left y-axis, barplot on right/secondary y-axis), then I get what I basically expect, but still unusable as barplot hides detail of all lineplots.

```julia
using Plots
using Plots.PlotMeasures
pyplot()
plot(rand(10), label="v1", right_margin=1cm)
plot!(rand(10) * 10, label="v2")
bar!(twinx(), rand(10), label="", c=:white)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/4/9/4941e6cea04f82db017c83791881c3e7c4d63f0f.png)

This happens on both GR and PyPlot backend, and also happens on both Plots v1.16.5 and v1.17.0 (latest).

I’m about to submit a bug ticket, but want to check if I’m not doing something stupid - please let me know if this happens in your environment. Also, if there’s any workaround, please let me know!

---

<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:** [July 12, 2021, 8:42am UTC](https://discourse.julialang.org/t/dual-axis-plot-breaks-with-multiple-variables-on-right-secondary-y-axis/64491/2 "2021-07-12T08:42:49Z")

</div>

Not sure if this is a bug but with the array syntax it seems to work fine:

```julia
using Plots; gr()
using Plots.PlotMeasures
bar(rand(10), c=:white, right_margin=1cm)
plot!(twinx(), [rand(10), rand(10) * 10], label=["v1" "v2"])

```

![Plots_gr_bar_line_multiaxes](https://global.discourse-cdn.com/julialang/original/3X/c/0/c05d07c7d11e1b1220cfb2480992bfb365d3e5af.png)

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [July 12, 2021, 9:13am UTC](https://discourse.julialang.org/t/dual-axis-plot-breaks-with-multiple-variables-on-right-secondary-y-axis/64491/3 "2021-07-12T09:13:09Z")

</div>

I am also not sure if it is a bug or not. But I also think it should be improved.

By the way, the problem of bars hiding line plots can be solved by `fillalpha=0`.

```julia
using Plots
using Plots.PlotMeasures
pyplot(fmt = :svg)
plot(rand(10), label="v1", right_margin=1cm)
plot!(rand(10) * 10, label="v2")
bar!(twinx(), rand(10), label="", fillalpha=0)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/5/d538f6f110f3ebad0ce3c42de79f79ff34d89ef3.jpeg)

---

<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:** [July 12, 2021, 10:43am UTC](https://discourse.julialang.org/t/dual-axis-plot-breaks-with-multiple-variables-on-right-secondary-y-axis/64491/4 "2021-07-12T10:43:44Z")

</div>

Every call to `twinx()` creates a new axis, so you want to re-use the first one:

```julia
julia> axis2 = twinx();

julia> plot!(axis2, rand(10), label="v1")

julia> plot!(axis2, rand(10), label="v2")

```

But to me using the `twinx()` stuff in Plots always feels like an uphill struggle - usually once my Plots get that complicated I switch to Makie…

---

<div class="post-metadata">

**Author:** ![tai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tai/32/5799_2.png) [@tai](https://discourse.julialang.org/u/tai)\
**Post date:** [July 13, 2021, 5:27am UTC](https://discourse.julialang.org/t/dual-axis-plot-breaks-with-multiple-variables-on-right-secondary-y-axis/64491/5 "2021-07-13T05:27:27Z")

</div>

Wow, thank you all for solutions/workarounds!

It brought me new understanding of twinx(), that it can be used to create not only 2, but multiple (N\>1) additional y-axis by giving different (x-)offset for each twinx()-ed y-axis. Also, while Makie is still foreign to me, it does have many stunning examples - I definitely should check it out.

---

<div class="post-metadata">

**Author:** ![thudjx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thudjx/32/15231_2.png) [@thudjx](https://discourse.julialang.org/u/thudjx)\
**Post date:** [September 3, 2021, 1:43am UTC](https://discourse.julialang.org/t/dual-axis-plot-breaks-with-multiple-variables-on-right-secondary-y-axis/64491/6 "2021-09-03T01:43:31Z")

</div>

Maybe a little bit divergent from this topic, but is Makie.jl easier to handle than Plots.jl? I am always struggling to use different backends for different intensions.
