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

x axis labeling disappears when using twinx() #127

Closed
bradyrx opened this issue Mar 3, 2020 · 11 comments · Fixed by #164
Closed

x axis labeling disappears when using twinx() #127

bradyrx opened this issue Mar 3, 2020 · 11 comments · Fixed by #164
Labels

Comments

@bradyrx
Copy link
Collaborator

bradyrx commented Mar 3, 2020

Code sample, a copy-pastable example if possible

A "Minimal, Complete and Verifiable Example" will make it much easier for maintainers to help you.

import proplot as plot
import numpy as np
import xarray as xr

times = np.arange(1990, 2000)
data = xr.DataArray(np.random.rand(len(times)), dims=['time'], coords=[times])

f, ax = plot.subplots(aspect=2)
ax.plot(data.time, data)

twinax = ax.twinx()

# twinax.format(xlocator=1)
ax.format(xticks=np.arange(1990, 2000, 2))

Actual result vs. expected result

Using twinx(), the tick labeling disappears on the twin axis. This happens for datetime and normal numbers. Using xticks/xlocator on the main axis or twinax instance changes the ticks but does not produce labels.

@bradyrx
Copy link
Collaborator Author

bradyrx commented Mar 3, 2020

You can actually see this in the RC settings demo on the docs on the right subplot.

https://proplot.readthedocs.io/en/latest/basics.html#Changing-rc-settings

@lukelbd
Copy link
Collaborator

lukelbd commented Mar 3, 2020

Thanks for the report -- which version are you using? Is this the "dev" release?

@bradyrx
Copy link
Collaborator Author

bradyrx commented Mar 3, 2020

I'm using the 0.0.5 that's on pip now.

@lukelbd lukelbd added bug support and removed bug labels Mar 3, 2020
@lukelbd
Copy link
Collaborator

lukelbd commented Mar 6, 2020

Actually this is intended behavior. My guess is you have confused twinx with twiny? Counterintuitively, the matplotlib twinx function generates two y axes with identical "twin" x axes, and ProPlot ensures the shared axes ticks, tick labels, and axis labels on the secondary x axis are hidden so you don't accidentally draw duplicate labels from both "twins" on top of each other. If you want to set the x axis properties, you should do so on the original axes.

Since this naming is really confusing for users, proplot introduces the more intutively-named altx function for making an "alternate x axes" and alty for making an "alternate y axes". I recommend using these instead.

Anyway maybe you already knew all this; since the whole purpose of "twin" axes is that their properties should be identical, I thought it made sense to forbid specially modifying the properties of the "twin". I'm open to arguments against this though.

@lukelbd lukelbd closed this as completed Mar 6, 2020
@zxdawn
Copy link

zxdawn commented Mar 7, 2020

@lukelbd I met the same problem today.

If I use the same example as @bradyrx showed above without twin, the xaxis tick labels are correct:
image

But, if I add the the twinx(), the tick labels disappeared:
image

This is the actual case I met: I want to plot two kinds of profiles with pressure, but I couldn't let the pressure ticks labels show up ...
image

@bradyrx bradyrx reopened this Mar 7, 2020
@bradyrx
Copy link
Collaborator Author

bradyrx commented Mar 7, 2020

Counterintuitively, the matplotlib twinx function generates two y axes with identical "twin" x axes, and ProPlot ensures the shared axes ticks, tick labels, and axis labels on the secondary x axis are hidden so you don't accidentally draw duplicate labels from both "twins" on top of each other. If you want to set the x axis properties, you should do so on the original axes.

Yes I understand this and agree with you that the naming is bad. I also agree that you shouldn't be able to modify the dual axis so you don't get out-of-phase ticks, clutter, etc.

The issue, as @zxdawn also mentioned, is that the original axis completely disappears. And in my example, you see that modifying the original axis does nothing to fix this (below with alty):

import proplot as plot
import numpy as np
import xarray as xr

times = np.arange(1990, 2000)
data = xr.DataArray(np.random.rand(len(times)), dims=['time'], coords=[times])

f, ax = plot.subplots(aspect=2)
ax.plot(data.time, data)

twinax = ax.alty()

# twinax.format(xlocator=1)
ax.format(xticks=np.arange(1990, 2000, 2))

Screen Shot 2020-03-07 at 10 40 37 AM

I agree you shouldn't be able to modify the twin axis, but it shouldn't render the original axis unreadable. It's still important to see the ticks, time series labeling, etc.

@lukelbd
Copy link
Collaborator

lukelbd commented Mar 7, 2020

My bad! I see now, this definitely is a major problem. I will address this before the end of the weekend. Thanks for the additional examples.

@lukelbd
Copy link
Collaborator

lukelbd commented May 13, 2020

Sorry for the wait guys; I had to step back from proplot for a while and knew this one would be tricky to fix. But it's finally done.

@zxdawn
Copy link

zxdawn commented May 13, 2020

@lukelbd Thanks for the fix! Will test it for the O3 and Qv profile tomorrow.

@bradyrx
Copy link
Collaborator Author

bradyrx commented May 13, 2020

This is great, thanks Luke!

@zxdawn
Copy link

zxdawn commented May 14, 2020

Works well. Thank Luke!
proplot_profile

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

Successfully merging a pull request may close this issue.

3 participants