JuliaPlots / Plots.jl

Powerful convenience for Julia visualizations and data analysis
https://docs.juliaplots.org
Other
1.84k stars 355 forks source link

Plot(twinx(), ...) creates ylabel on the wrong side of the plot (left instead of right) #1276

Closed LotteVictor closed 6 years ago

LotteVictor commented 6 years ago

In my question about visualization on the julia discourse website (https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558) an issue occured that was identified as a bug by @mkborregaard: When using plot(twinx()...) the ylabel does not appear on the right side (as it should) but on the left side, on top of the original ylabel.

PhilipVinc commented 6 years ago

I also bumped into this. I hope someone fixes it..

metorm commented 6 years ago

Guys, I am faced with this problem, too.

PhilipVinc commented 6 years ago

I would gladly address this if someone pointed me in the right direction.

Upon inspection, twinx() does correctly set the :ymirror=true attribute, but it seems that GR.jl only mirrors the ticks and not the label.

If you try to run

using Plots
t=collect(1:10);
x=rand(10);
plot(t, x, ymirror=true, ylabel="ylab")

you will see that the plot has no mirrored label. Therefore I think that this is a bit of a deeper problem in GR(). PyPlot works somewhat fine.

mkborregaard commented 6 years ago

It is likely not in GR, but in the backends/gr.jl file of Plots

PhilipVinc commented 6 years ago

@mkborregaard Thanks. You were right: It was a very easy fix. It's actually a two line fix.

# Line 906
if xaxis[:guide_position] == :top
...

# Line  918
if yaxis[:guide_position] == :left

should become

if xaxis[:guide_position] == :top || (xaxis[:guide_position] == :auto && xaxis[:mirror] == true)
...
if yaxis[:guide_position] == :right || (yaxis[:guide_position] == :auto && yaxis[:mirror] == true)

Could you point me out towards the best way to commit it back to the community? Should I create a PR?

daschw commented 6 years ago

Yes, please submit a PR! If you need guidance, feel free to ask or read http://docs.juliaplots.org/latest/contributing/#git-fu-or-the-mechanics-of-contributing

PhilipVinc commented 6 years ago

I submitted a PR. Moreover, I took the liberty of changing the code that computes the padding to add the padding to the side where the label is actually located.

Please let me know if the PR is not well done or if I should do something else.

adannenberg commented 3 years ago

Does this now work for others? It doesn't fully work for me... When I run

using Plots
x=1:20
y1=rand(length(x))
y2 = sin.(x)
plot(x,y1,label = "My y1 label", legend = :topleft, ylabel = "the lh y-axis",grid=:off)
plot!(twinx(),y2, label = "my y2 label", legend=:topright, ylabel = "the rh y-axis", grid=:off, xlabel="numbers", color=:red, box=:on)

I get a plot with no label on the right hand y-axis. I'm not sure if it's off the plot to the right or missing (less likely). Apologies for not using the "Insert code" tool. When I used it and previewed this message it seemed to ignore newlines...

Edit: It's also the case that a graph such as the one above cannot be included as a subgraph with layout. If Ichange the code above:

using Plots
x=1:20
y1=rand(length(x))
y2 = sin.(x)
p1=plot(x,y1,label = "My y1 label", legend = :topleft, ylabel = "the lh y-axis",grid=:off)
plot!(p1,twinx(),y2, label = "my y2 label", legend=:topright, ylabel = "the rh y-axis", grid=:off, xlabel="numbers", color=:red, box=:on)
p2 = plot(1:10,1:10)
plot(p1,p2, layout = (1,2)

then I get an error: LoadError: Cannot convert Plots.Subplot{Plots.GRBackend} to series data for plotting

This is true in both Atom+Juno and Pluto. Not sure about different backends (although I know the rh y-axis problem is there with Plotly as well as Gr).

kosukesando commented 4 months ago

I have a similar problem, where the labels are fine but the ticks for the twinx axis are duplicated on the left side, where I only want it on the right side. image Let me know if I should make a new issue for this.

EDIT:

Ok so turns out adding framestyle=:semi to the twinx axis gets rid of the redundant ticks. However this is at the cost of (?) the border at the top, which I just noticed already happens regardless of this. Probably going to make a new issue now, but will keep this in case someone needs this info.