The Wayback Machine - https://web.archive.org/web/20220101043915/https://github.com/matplotlib/matplotlib/issues/9970
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

clf() and cla() throws warnings with shared loglog plots. #9970

Open
fredrik-1 opened this issue Dec 10, 2017 · 8 comments
Open

clf() and cla() throws warnings with shared loglog plots. #9970

fredrik-1 opened this issue Dec 10, 2017 · 8 comments

Comments

@fredrik-1
Copy link
Contributor

@fredrik-1 fredrik-1 commented Dec 10, 2017

import matplotlib.pyplot as plt

fig=plt.figure(1)
ax1=fig.add_subplot(1,2,1)
ax1.loglog(range(10))
ax2=fig.add_subplot(1,2,2, sharex=ax1)
ax2.loglog(range(10))

fig.clf()

Throws a warning:
UserWarning: Attempted to set non-positive xlimits for log-scale axis; invalid limits will be ignored.
'Attempted to set non-positive xlimits for log-scale axis;

ax1.cla() also throws a warning.

Matplotlib version
2.1.0,
python 3.6.3
ipython 6.1.0

@lkjell
Copy link
Contributor

@lkjell lkjell commented Dec 11, 2017

fig.clf() will do a ax1.cla(). So the real culpit is ax1.cla().
Because ax1 does not have a master/parent axes share it will default to 'linear' scale. And then set the data limit to [0,1]. But since ax1 share axes with ax2 which has log scale it will throw the warning.

So there are 2 problems here. 1) Why should fig.clf() clear axes as well. It should just clear the axes from the figure and not clear the axes itself.
2) when a master axes clear should it revert all the other shared axes to linear scale as well.

@divenex
Copy link

@divenex divenex commented Apr 17, 2018

This bug is still present in Matplotlib 2.2

@divenex
Copy link

@divenex divenex commented Jul 1, 2021

This bug still persists in Matplotlib 3.3

@jklymak
Copy link
Member

@jklymak jklymak commented Jul 1, 2021

You are trying to plot 0 on a loglog plot so you get a warning. I am not sure what the bug is.

@timhoffm
Copy link
Member

@timhoffm timhoffm commented Jul 1, 2021

You are trying to plot 0 on a loglog plot so you get a warning. I am not sure what the bug is.

Yes, but probably other than you think 😄. This is the minimal example I cam up with (no 0 data involved):

fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True)
x = range(1, 10)
ax1.plot(x, x)
ax2.semilogx(x, x)
ax1.cla()

Ingedients you need:

  • at least one log scale on a shared axis
  • clearing an Axes that axis-shares with the log Axes.

I suspect this has to do something with

  1. an internal state not being updated (missing show()) so that we still have a 0 limit.
  2. The interaction between shared axes during cleanup.

Given that this only happens when you clear a non-shown figure (that also needs certain further properties), I'd be fine with closing as "won't fix".

@anntzer
Copy link
Contributor

@anntzer anntzer commented Jul 2, 2021

Actually the warning shows up even if the figure is shown (e.g. if you type @timhoffm's lines one after the other at an IPython prompt). AFAICT the root cause is that cla()'ing ax1 resets it to linear, but doesn't reset the scale on ax2, so we end up with ax1 using log and ax2 using linear scale, which seems contrary to the semantics of shared axes.

Also there's some funky asymmetry which makes ax2.cla() not do the same (it doesn't even revert the scale to linear), likely because the implementation in cla is

        if self._sharex is not None:
            self.sharex(self._sharex)
        else:
            self.xaxis._set_scale('linear')
            try:
                self.set_xlim(0, 1)
            except TypeError:
                pass

so things behave differently between the master axes (which has self._sharex=None) and the follower axes (which as self._sharex = master); at least that seems like something to be fixed...

@Fernal73
Copy link

@Fernal73 Fernal73 commented Aug 30, 2021

import warnings
warnings.filterwarnings("ignore", message="Attempted to set non-positive left xlim on a log-scaled axis.\nInvalid limit will be ignored.")

Ignore the warning as above.

@billtubbs
Copy link

@billtubbs billtubbs commented Jan 1, 2022

It would be great to have this fixed! It's raising warnings in our test script which is like this:

fig, axes = plt.subplots(2, 1, sharex=True)
axes[0].loglog([1e-1, 1e0, 1e1], [10, 100, 1000])
axes[1].loglog([1e-1, 1e0, 1e1], [10, 100, 1000])
# ... do some tests here ...
fig.clear()
UserWarning: Attempted to set non-positive left xlim on a log-scaled axis.
Invalid limit will be ignored.
  fig.clear()

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

Successfully merging a pull request may close this issue.

None yet
8 participants