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

axis limits don't coordinate with each other #13648

Closed
jsh9 opened this issue Mar 11, 2019 · 11 comments
Closed

axis limits don't coordinate with each other #13648

jsh9 opened this issue Mar 11, 2019 · 11 comments
Labels
Difficulty: Medium Documentation Good first issue Open a pull request against these issues if there are no active ones!

Comments

@jsh9
Copy link

jsh9 commented Mar 11, 2019

Bug report (or feature request)

Summary

In matplotlib, when manually restricting xlim, ylim does not change accordingly.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1,2,3,4,5,6,7])
y = x

fig = plt.figure()
ax = plt.axes()
ax.plot(x, y)
print('xlim: %s, ylim: %s' % (ax.get_xlim(), ax.get_ylim()))

fig = plt.figure()
ax = plt.axes()
ax.plot(x, y)
ax.set_xlim(1, 2)
print('xlim: %s, ylim: %s' % (ax.get_xlim(), ax.get_ylim()))

Actual outcome

xlim: (0.7, 7.3), ylim: (0.7, 7.3)
xlim: (1.0, 2.0), ylim: (0.7, 7.3)

Expected outcome

When I change xlim to (1, 2), I expect ylim to change accordingly (because y's value range becomes [1, 2], now that x's range shrinks), but ylim still remains (0.7, 7.3). In fact, in MATLAB xlim and ylim can coordinate with each other.

MATLAB codes to reproduce this behavior:

x = [1,2,3,4,5,6,7];
y = x;
figure; plot(x, y); disp(ylim);
figure; plot(x, y); xlim([1, 2]); disp(ylim);

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 2.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.6.6
  • Jupyter version (if applicable): N/A
  • Other libraries: N/A
@WeatherGod
Copy link
Member

WeatherGod commented Mar 11, 2019 via email

@jklymak
Copy link
Member

jklymak commented Mar 11, 2019

... I think there are just as many people who would complain about that behaviour as might appreciate it. I used Matlab for many years and don't recall it doing this, but perhaps I've forgoten.

@jsh9
Copy link
Author

jsh9 commented Mar 11, 2019

@WeatherGod Yes, that is what I meant. I did try to set autoscale (https://matplotlib.org/api/_as_gen/matplotlib.pyplot.autoscale.html), but I could not get it to work due to lack of an official example.

@jklymak I think matplotlib can offer users the option to turn it on and off easily.

@anntzer
Copy link
Contributor

anntzer commented Mar 11, 2019

This was discussed and rejected in #9890. It would also require a pretty deep rewrite of the way data limits are computed.

@jsh9
Copy link
Author

jsh9 commented Mar 11, 2019

@anntzer So autoscale and set_autoscale_view currently just do nothing? (I tried autoscale and the poster in #9890 tried set_autoscale_view.)

If they are not for this purpose, then I would suggest an update on the official documentation, because it is quite vague to a user as currently written:

"matplotlib.pyplot.autoscale(enable=True, axis='both', tight=None)

Autoscale the axis view to the data (toggle).

Convenience method for simple axis view autoscaling. It turns autoscaling on or off, and then, if autoscaling for either axis is on, it performs the autoscaling on the specified axis or axes."

@WeatherGod
Copy link
Member

WeatherGod commented Mar 11, 2019 via email

@jsh9
Copy link
Author

jsh9 commented Mar 11, 2019

Thanks for the explanation. I understand it would be a lot of work to implement this. So I guess a good compromise is to note this default in the documentation.

@WeatherGod
Copy link
Member

WeatherGod commented Mar 11, 2019 via email

@jsh9
Copy link
Author

jsh9 commented Mar 11, 2019

With this short script below, I can confirm two different behaviors:

  1. By setting autoscale to "on", I allow the y axis bounds to automatically stretch to make room for out-of-bound data points.
  2. The y axis bounds do not automatically shrink with data, even if I set autoscale to on.

However, by reading the current documentation (https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.autoscale.html), I could only expect Behavior No. 1, but not Behavior No. 2. So this is where I think the documentation can benefit from some improvements.

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1,2,3,4,5,6,7])
y = x

fig = plt.figure()
ax = plt.axes()
ax.plot(x, y)
print('xlim: %s, ylim: %s' % (ax.get_xlim(), ax.get_ylim()))

ax.autoscale(enable=False)
y1 = 2 * x
ax.plot(x, y1)  # ylim is not allowed to stretch
print('xlim: %s, ylim: %s' % (ax.get_xlim(), ax.get_ylim()))

ax.autoscale(enable=True)
y2 = 2 * x + 1
ax.plot(x, y2)  # ylim stretches automatically
print('xlim: %s, ylim: %s' % (ax.get_xlim(), ax.get_ylim()))

ax.autoscale(enable=True)
y3 = 2 * x + 2
ax.set_xlim(1, 2)
ax.plot(x, y2)  # ylim does not shrink automatically
print('xlim: %s, ylim: %s' % (ax.get_xlim(), ax.get_ylim()))

@tacaswell tacaswell added this to the future releases milestone Nov 1, 2022
@tacaswell
Copy link
Member

This issue is still valid.

The work is to add another paragraph to the autoscale docstring so that the observed behavior in #13648 (comment) is not surprising

@tacaswell tacaswell added Good first issue Open a pull request against these issues if there are no active ones! Difficulty: Medium labels Nov 1, 2022
@QuLogic
Copy link
Member

QuLogic commented Mar 8, 2023

Closing as a duplicate of #2123.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium Documentation Good first issue Open a pull request against these issues if there are no active ones!
Projects
None yet
Development

No branches or pull requests

6 participants