Skip to content

[Bug]: Calling axes.clear() on a shared y or x-axis issues an unexpected warning with log scale #30159

Open

Description

@fwitte

Bug summary

Hi everyone,

I somehow created a plot in unorthodox order, i.e. first plot some data in the second column, then in the first one. To generate my plots, I call a method that automatically

  • clears the axes
  • sets the scale
  • sets the limits
  • plots the data

In this case two warnings are issued. The code of the minimal example reproduces this behavior. When changing it to the second variant in the code block (first plotting the first column, then the second column) the issue disappears.

Thank you!

Best

Francesco

Code for reproduction

import numpy as np
from matplotlib import pyplot as plt


# ISSUE ARISES IN THIS VARIANT
fig, ax = plt.subplots(1, 2, sharey=True)


ax[1].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
ax[1].set_yscale("log")
ax[1].set_ylim([1e0, 1e2])

ax[0].clear()
ax[0].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))

plt.show()

# NO ISSUE IN THE VARIANT BELOW
import numpy as np
from matplotlib import pyplot as plt


fig, ax = plt.subplots(1, 2, sharey=True)


ax[0].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
ax[0].set_yscale("log")
ax[0].set_ylim([1e0, 1e2])

ax[1].clear()
ax[1].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))

plt.show()

Actual outcome

FutureWarning: Attempt to set non-positive ylim on a log-scaled axis will be ignored.
FutureWarning: Attempting to set identical low and high ylims makes transformation singular; automatically expanding.

Expected outcome

No warning is issued

Additional information

No response

Operating system

Windows

Matplotlib Version

3.10.0

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.11.11

Jupyter version

No response

Installation

pip

Activity

  1. fwitte commented on Jun 10, 2025

    @fwitte
    Author

    To add:

    With a grid of plots, the issue only appears, when the very first axes (top left) is cleared.

    No warning issued:

    fig, ax = plt.subplots(2, 2, sharey=True)
    
    
    ax[0, 1].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
    ax[0, 1].set_yscale("log")
    ax[0, 1].set_ylim([1e0, 1e2])
    
    ax[1, 1].clear()
    ax[1, 1].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
    
    fig, ax = plt.subplots(2, 2, sharey=True)
    
    
    ax[1, 1].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
    ax[1, 1].set_yscale("log")
    ax[1, 1].set_ylim([1e0, 1e2])
    
    ax[1, 0].clear()
    ax[1, 0].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
    
    fig, ax = plt.subplots(2, 2, sharey=True)
    
    
    ax[1, 1].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
    ax[1, 1].set_yscale("log")
    ax[1, 1].set_ylim([1e0, 1e2])
    
    ax[0, 1].clear()
    ax[0, 1].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
    

    Warning issued:

    fig, ax = plt.subplots(2, 2, sharey=True)
    
    
    ax[1, 1].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
    ax[1, 1].set_yscale("log")
    ax[1, 1].set_ylim([1e0, 1e2])
    
    ax[0, 0].clear()
    ax[0, 0].scatter(np.random.randint(1, 100, 100), np.random.randint(1, 100, 100))
    
  2. jklymak commented on Jun 10, 2025

    @jklymak
    Member

    This does seem to be a bug in that the yscale is shared in the second case, but not in the first.

  3. rcomer commented on Jun 10, 2025

    @rcomer
    Member

    Maybe caused by #27825

  4. mrinalcodez commented on Jul 21, 2025

    @mrinalcodez

    It seems like the master and follower plots have become parent and children plots. I think it would be better if when we call ax.clear() on any of the plots we can prevent it from changing the scale and limits from defaulting and this will result in the plots being tightly bounded to each when shared

  5. timhoffm commented on Jul 21, 2025

    @timhoffm
    Member

    clear() semantics for shared Axes is not well defined. See also #28851 (comment).

    As an implementation detail, sharing is implemented asymmetrically.

    In [1]: fig, axs = plt.subplots(1, 2, sharey=True)
    
    In [2]: axs[0]._sharey is None
    Out[2]: True
    
    In [3]: axs[1]._sharey is axs[0]
    Out[3]: True

    The unclear semantics results in different behaviors on both Axes:

    • axs[1].clear() leaves the scale and axes limits unchanged
    • axs[0].clear() resets the scale and axes limits on axs[0]. It leaves the scale on axs[1] but tries to propagate the reset axes limits from 1 to 2 causing the warning.

    The quick fix for more consistency would be to make axs[0].clear() also leave scale and limits unchanged. This would be more consistent, but not necessarily a desired behavior (see #28851 (comment)).

  6. timhoffm commented on Nov 16, 2025

    @timhoffm
    Member

    I think a reasonable incremental improvement would be to remove the Axes._sharex/y state. It's only used in very few places and can more reasonably be handled via the grouper Axes._shared_axes['x'].

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions