Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LotteVictor opened this issue Dec 4, 2017 · 9 comments
Labels

Comments

@LotteVictor
Copy link

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
Copy link
Contributor

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

@metorm
Copy link

metorm commented Oct 23, 2018

Guys, I am faced with this problem, too.

@PhilipVinc
Copy link
Contributor

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
Copy link
Member

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

@PhilipVinc
Copy link
Contributor

PhilipVinc commented Oct 23, 2018

@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
Copy link
Member

daschw commented Oct 23, 2018

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
Copy link
Contributor

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
Copy link

adannenberg commented Jan 3, 2021

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
Copy link

kosukesando commented Jun 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants