Thanks to visit codestin.com
Credit goes to github.com

Skip to content

xlim is not auto scaled when using twinx #16723

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

Closed
01baftb opened this issue Mar 10, 2020 · 7 comments
Closed

xlim is not auto scaled when using twinx #16723

01baftb opened this issue Mar 10, 2020 · 7 comments
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action

Comments

@01baftb
Copy link

01baftb commented Mar 10, 2020

I am reporting as possible bug.

When plotting a secondary y axis on the right, the xlim is not being properly auto scaled. As possible work around was proposed here, https://stackoverflow.com/a/27344398/4988010, however, it seems this was fixed here, 26ed35b. However, I am not seeing the issue being resolved on matplotlib v3.2.0.

%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
decay = lambda C, K, x: C *(1-np.exp(-1*K*x))
x = np.linspace(0,10,10)

# Create a secondary y axis on the right
twin_ax = ax.twinx()

for n in range(5):
    C = n**2 + 5
    K = 0.75
    # Plot the exponential decay on left y axis 
    ax.plot(x,decay(C,K,x), marker='o', linestyle='--', label=C)
    # Plot the limit of decay on right y axis 
    twin_ax.axhline(y=C, color='gray') 
    
ax.legend()
ax.set_xlabel('X')
ax.set_ylabel('Measured values')
twin_ax.set_ylabel('Theoritical limit')

# Align the both left and right y axis 
twin_ax.set_ylim(ax.get_ylim())

##################
# Issues or bugs
# Need to fix the X limits or else 
# the graph doens't display properly
##################

print('xlim before fix: {}'.format(ax.get_xlim()))
ax.relim()
print('xlim after relim(): {}'.format(ax.get_xlim()))
# A hack to fix the issue # https://stackoverflow.com/a/27344398/4988010
twin_ax.plot(0, 0, visible=False)
print('xlim after fixing: {}'.format(ax.get_xlim()))

plt.show()

plot with issue
plot with working hack

@01baftb 01baftb changed the title xlim is not autoscaled when using twinx xlim is not auto scaled when using twinx Mar 10, 2020
@jklymak
Copy link
Member

jklymak commented Mar 10, 2020

This looks like the axes are fighting each other and axhline is hard coding the axes limits because folks argued it should have a role in setting the view limits.

Possible solutions are to plot your lines on the original axes, or use ax.secondary_axes instead of twin_axes. Is there are reason you are using twin_axes here?

@ImportanceOfBeingErnest
Copy link
Member

It's still a regression, right?

image

image

@jklymak
Copy link
Member

jklymak commented Mar 10, 2020

Sure, bisects to 84448ca #15258 But that fixed something else that was broken.

@01baftb
Copy link
Author

01baftb commented Mar 11, 2020

This looks like the axes are fighting each other and axhline is hard coding the axes limits because folks argued it should have a role in setting the view limits.

Possible solutions are to plot your lines on the original axes, or use ax.secondary_axes instead of twin_axes. Is there are reason you are using twin_axes here?

Thanks for pointing out ax.secondary_axes. This seems to be a new feature I just found out about. However, I am still unsure how I can use this to plot a secondary y axis on the right for the specific example I shared. For example, when I try to do the following, I an error,

AttributeError: 'SecondaryAxis' object has no attribute 'axhline'.

twin_ax = ax.secondary_yaxis('right')
twin_ax.axhline(y=10, color='gray') 

With limited use examples/documentation, I am still unsure what is the different between secondary_yaxis() and twinx().

@jklymak
Copy link
Member

jklymak commented Mar 11, 2020

Sure, a bit off topic, but secondary axis is not to be drawn on, its just to show an alternate scale. All the plotting happens on the parent axes.

@oscargus
Copy link
Member

I think this is fixed now.

16723

Should probably add a test though before closing.

@github-actions
Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Oct 11, 2023
@github-actions github-actions bot added the status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. label Nov 10, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action
Projects
None yet
Development

No branches or pull requests

4 participants