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

Unable to disable autoscaling in pyplot.plot #17955

Open
neilpeterman opened this issue Jul 17, 2020 · 7 comments
Open

Unable to disable autoscaling in pyplot.plot #17955

neilpeterman opened this issue Jul 17, 2020 · 7 comments

Comments

@neilpeterman
Copy link

Bug report

Bug summary

Arguments to disable autoscaling are not working, so despite e.g. scalex=False the horizontal axis is re-scaled.

Code for reproduction

import matplotlib.pyplot as plt

plt.plot([0, 2], [0, 2], '-')
plt.plot([0, 10], [1, 1], '-', scalex=False)

Actual outcome

Output in-line in jupyter notebook:
image

Expected outcome

Expected autoscaling only for the first call, so the xscale is just a bit padded from [0, 2]. This works as expected in matplotlib==3.1.3:
image

Matplotlib version

  • Operating system: Ubuntu 18.04.3
  • Matplotlib version: 3.3.0
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.6.8
  • Jupyter version (if applicable): 1.0.0
  • Other libraries: N/A

Tools installed with pip

@QuLogic
Copy link
Member

QuLogic commented Jul 18, 2020

This comes from 160de56, or #13593. This may be an expected change; see the API note from that PR.

cc @anntzer

@neilpeterman
Copy link
Author

Thanks for the clarification @QuLogic -- so the way to achieve the previous behavior would be to do something like this:

plt.plot([0, 2], [0, 2], '-')
plt.gcf().canvas.draw()
plt.plot([0, 10], [1, 1], '-', scalex=False)

The concept of delaying autoscaling makes sense to me, although in the example above the meaning of scalex=False is not so intuitive to me -- I would think anything with scalex=False should be excluded from the autoscaling process in one way or another. Does that sound like a reasonable expectation?

I'm not sure -- it could be that I'm just accustomed to the previous behavior.

@anntzer
Copy link
Contributor

anntzer commented Jul 18, 2020

I guess what you really want is more along the lines of #15595?
In the meantime we could trigger an autoscale when scalex/scaley goes from True to False, i.e. in _request_autoscale_view, something like if self._stale_viewlim_x and not scalex or self._stale_viewlim_y and not scaley: self._unstale_viewlim()? (untested, but hopefully you get the idea...)

@neilpeterman
Copy link
Author

It seems like the fixes you bring up would address the issue in a reasonable way, triggering autoscaling when needed.

@igurin-invn
Copy link
Contributor

Just a little poke: this bug is still open. Whether or not it results from an intentional change in #13593, it's not reflected in the documentation.

scalex, scaleybool, default: True
These parameters determine if the view limits are adapted to the data limits. The values are passed on to autoscale_view.

@anntzer
Copy link
Contributor

anntzer commented Jun 3, 2023

Good point. I think scalex/scaley should be deprecated in favor of #15595 once something like that gets in, but in the meantime the following seems to work OKish, going back to the old semantics:

diff --git i/lib/matplotlib/axes/_axes.py w/lib/matplotlib/axes/_axes.py
index 5369eadbde..2b08537ef1 100644
--- i/lib/matplotlib/axes/_axes.py
+++ w/lib/matplotlib/axes/_axes.py
@@ -1719,6 +1719,8 @@ class Axes(_AxesBase):
         additionally use any  `matplotlib.colors` spec, e.g. full names
         (``'green'``) or hex strings (``'#008000'``).
         """
+        if not scalex or not scaley:
+            self._unstale_viewLim()
         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
         lines = [*self._get_lines(*args, data=data, **kwargs)]
         for line in lines:

(note that using scalex/scaley=False then implies some runtime cost, as explained in 160de56)

@igurin-invn
Copy link
Contributor

Is there a better workaround than this?

_xlim = ax.get_xlim()
ax.plot(x_big, y)
ax.set_xlim(_xlim)

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

No branches or pull requests

4 participants