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

Skip to content

Axes with sharex can have divergent axes after setting tick markers #8946

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
phil20686 opened this issue Jul 27, 2017 · 8 comments · Fixed by #18529
Closed

Axes with sharex can have divergent axes after setting tick markers #8946

phil20686 opened this issue Jul 27, 2017 · 8 comments · Fixed by #18529

Comments

@phil20686
Copy link

Bug report

When creating two axes using:

ax1 = fig.add_axes([0.1,0.5,0.8, 0.4])
ax2 = fig.add_axes([0.1,0.1,0.8, 0.4], sharex=ax1)

It is expected that the return values of ax1.get_xlim() and ax2.get_xlim() should always be identical. I.e. that the two axes share a single domain. This bug shows an example where these diverge.

Code for reproduction

Run the following snippet:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = fig.add_axes([0.1,0.5,0.8, 0.4])
ax2 = fig.add_axes([0.1,0.1,0.8, 0.4], sharex=ax1)

data = np.random.randn(10)
data2 = np.random.randn(10)
index = range(10)
index2 = [x+5.5 for x in index]

ax1.scatter(index, data)
ax2.scatter(index2, data2)

ax1.set_xticks(range(-10, 20, 1))

print ax1.get_xlim()
print ax2.get_xlim()

and the return is :
(-10.0, 19.0)
(-2.0, 16.0)

when it is expected that both should be (-10.0, 19.0)

  • Operating System: Windows
  • Matplotlib Version: 1.5.1
  • Python Version: 2.7.12

pip install of matplotlib

PS: Your template for issues is very helpful!

@dstansby
Copy link
Member

Thanks for providing an easy to copy/paste example. I can confirm this is a problem on the master branch, with python 3.6. Adding plt.show to the end of that and then moving the axis slightly makes the limits correct, but as you say they should be correct even before the figure is drawn.

@dstansby dstansby added this to the 2.1 (next point release) milestone Jul 27, 2017
@tacaswell tacaswell modified the milestones: 2.2 (next next feature release), 2.1 (next point release) Jul 28, 2017
@tacaswell
Copy link
Member

@dstansby Can you render a figure with miss-matched limits?

@dstansby
Copy link
Member

The figure shown by calling plt.show() at the end of the above code block has mis-matched limits.

shaynair pushed a commit to shaynair/matplotlib that referenced this issue Mar 2, 2018
shaynair pushed a commit to shaynair/matplotlib that referenced this issue Mar 5, 2018
@jklymak jklymak modified the milestones: needs sorting, v3.0 Mar 5, 2018
@tacaswell tacaswell modified the milestones: v3.0, v3.1 Aug 11, 2018
@tacaswell tacaswell modified the milestones: v3.1.0, v3.2.0 Mar 18, 2019
@timhoffm timhoffm modified the milestones: v3.2.0, v3.3.0 Aug 17, 2019
@timhoffm
Copy link
Member

If somebody picks this up, this would be a good test (adapted from #10691):

def test_shared_axes_retick():
    fig, axs = plt.subplots(2, 2, sharex='all', sharey='all')

    for ax in axs.flat:
        ax.plot([0, 2], 'o-')

    axs[0, 0].set_xticks([-0.5, 0, 1, 1.5])  # should affect all axes xlims
    for ax in axs.flat:
        assert ax.get_xlim() == axs[0, 0].get_xlim()

    axs[0, 0].set_yticks([-0.5, 0, 2, 2.5])  # should affect all axes ylims
    for ax in axs.flat:
        assert ax.get_ylim() == axs[0, 0].get_ylim()

@jklymak
Copy link
Member

jklymak commented Sep 21, 2020

As per my comment in #18529, this is fine, and I can see why we'd expect the axes to match. However, fi we change this, the converse - where the user cannot explicitly set one set of ticks, but not the others, becomes impossible. I think its pretty easy for a user to loop through the axes and explicitly set them, rather than us doing it automatically, and making it impossible for them to be different.

Over all, does "sharing" an axes mean that everything is the same about the axes? I think the major convenience is having zooming linked when looking at the plot interactively, so that when I zoom on one axes, it zooms on the other. I'm less interested in the decorations on the axes precisely tracking one another, particularly if the user specifies them manually.

@l-johnston
Copy link
Contributor

@jklymak the issue is labeled 'confirmed bug' and has assigned milestone. What's the preferred way of initiating work on an issue like this?

@jklymak
Copy link
Member

jklymak commented Sep 21, 2020

@l-johnston Not your fault at all - after some thought, I'm just not sure I agree that this is really something we want to do. I actually have it on today's dev call to quickly decide...

@jklymak
Copy link
Member

jklymak commented Sep 21, 2020

OK, dev call today it was decided that this is the intended behaviour. The fix is believed to be easier than #18529...

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

Successfully merging a pull request may close this issue.

7 participants