Skip to content

Fix FigureImage losing its transform when re-added via add_artist - #32380

Open
ahmed5145 wants to merge 2 commits into
matplotlib:mainfrom
ahmed5145:fix-figimage-readd
Open

ahmed5145 wants to merge 2 commits into
matplotlib:mainfrom
ahmed5145:fix-figimage-readd

Conversation

@ahmed5145

Copy link
Copy Markdown
Contributor

PR summary

Closes #31730.

Figure.figimage() creates a FigureImage without setting a transform. Its offsets are in pixels, so it only drew correctly because an unset transform defaults to identity. After .remove() and fig.add_artist(im), add_artist sees is_transform_set() is False and gives the image transSubfigure, so the image is drawn in the wrong place and doesn't show up.

FigureImage.__init__ now sets IdentityTransform() explicitly, which is what @rcomer suggested in the issue. This covers both figimage() and a FigureImage created directly, and a transform= kwarg still overrides it. The other half of the issue (draw order relative to the axes) was already fixed by #31746, so with both changes the remove/re-add cycle now draws correctly.

AI Disclosure

I used Claude Code (generative AI) to trace the cause through add_artist, draft the regression test, and help write this PR description (to make sure I am following standards).

PR quality check

  • Use an expressive title.
  • New and changed code is tested
  • Plotting related features are demonstrated in an example
  • New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

N/A for example/release notes/docs: this is a bug fix that makes an existing workflow behave as expected, with no API change.

Comment thread lib/matplotlib/tests/test_image.py Outdated

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Figure.add_artist() fails to re-render an image previously added via Figure.figimage() after .remove()

3 participants