Skip to content

[ENH]: Remove artists from axes completely. #25168

Open

Description

@komoto48g

Problem

Calling art.remove() doesn't seem to completely detach the art from the original axes.
In the following sample code, when removing the art from axes (say ax1) and adding it to other axes (say ax2), the ax2 will not draw the art.

import numpy as np
from matplotlib import patches
from matplotlib import pyplot as plt

data = np.random.rand(5,5)

fig = plt.figure()
ax1, ax2 = fig.subplots(1,2)
ax1.imshow(data, interpolation='nearest', cmap='gray',)
ax2.imshow(data, interpolation='bilinear', cmap='gray',)
if 1:
    art = patches.Circle((2, 2), 2, fill=0)
    ax1.add_artist(art)
    art.remove()
    ax2.add_artist(art)
plt.show()

This issue was raised on: Discrouse matplotlib.

Proposed solution

The following line is suggested to be added after `art.remove()':

art.set_transform(ax2.transData)

or

art._transformSet = False

should be added in the art.remove method.

Activity

  1. jklymak commented on Feb 7, 2023

    @jklymak
    Member

    I think the second solution makes sense, and it not being set False almost seems like a bug. @komoto48g do you have bandwidth for a PR? The tricky part is that it will probably require a test.

    Anyone else understand the artist removal procedures well enough to comment if this makes sense? It seems correct to me...

  2. komoto48g commented on Feb 8, 2023

    @komoto48g
    Author

    Thank you for the advice. Sure. I will make this PR this weekend.

  3. tacaswell commented on Feb 13, 2023

    @tacaswell
    Member

    #24626 is an alternate approach. I am actually surprised that this does not raise...

  4. github-actions commented on Apr 13, 2026

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  5. added this to the v3.12.0 milestone on Apr 13, 2026
  6. self-assigned this
    on Apr 13, 2026
  7. timhoffm commented on Apr 13, 2026

    @timhoffm
    Member

    Do we want/need to guarantee that an artist that has been removed can be re-added? Is there a use case? We could alternatively explicitly document that this is not supported and users should create a new Artist if they need it again.

  8. QuLogic commented on Apr 17, 2026

    @QuLogic
    Member

    Since we've now made it so that Figure can be re-opened/re-attached to a GUI, it does seem like re-usability isn't unexpected. I don't have a particular use case or idea of the scope of work to guarantee this ability though, while @tacaswell already has the PR made to disallow it.

  9. timhoffm commented on Apr 17, 2026

    @timhoffm
    Member

    Switching backends of figures / attaching/detaching from pyplot is conceptually different than removing and attaching artists. I would not say that one justifiably gives rise to expectations on the other. The critical point with reusability is that state management has to be clean, in particular:

    • clearly decide which properties should be set on __init__, because that does only happen once
    • ensure removal cleans up all dependencies, not only refrences, but also state that was set based on the Figure/Axes
    • ensure reattaching wires all need dependencies

    We can of course, implement this, but it will be a substantial effort. The existing code was not written with that in mind, and I suppose there are quite a number of traps. As an example, take this code, which is called as part of add_artist():

    if not a.is_transform_set():
    a.set_transform(self.transData)

    I am of the opinion that this is not a priority and not worth the effort.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions