Skip to content

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

@williamlus

Description

@williamlus

Bug summary

After calling .remove() on the FigureImage object returned by Figure.figimage(...) and then re-attaching it via Figure.add_artist(...), the image is no longer rendered in the saved figure. The same remove/re-add cycle works correctly for a Text artist created via Figure.text(...), suggesting figimage objects are not properly handled by Figure.add_artist.

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from io import BytesIO

def text_to_rgba(s, *, dpi, **kwargs):
    fig = Figure(facecolor="none")
    fig.text(0, 0, s, **kwargs)
    with BytesIO() as buf:
        fig.savefig(buf, dpi=dpi, format="png", bbox_inches="tight",
                    pad_inches=0)
        buf.seek(0)
        rgba = plt.imread(buf)
    return rgba

# --- Case 1: figimage ---
fig, ax = plt.subplots()
rgba1 = text_to_rgba(r"IQ: $\sigma_i=15$", color="blue", fontsize=10, dpi=200)
figimg = fig.figimage(rgba1, 100, 50)
plt.savefig("figimage_before.png")   # image visible (correct)
figimg.remove()
fig.add_artist(figimg)
plt.savefig("figimage_after.png")    # image MISSING (bug)

# --- Case 2: text (control) ---
fig, ax = plt.subplots()
mytext = fig.text(0.2, 0.15, r"IQ: $\sigma_i=15$", color="blue", fontsize=20)
plt.savefig("text_before.png")       # text visible (correct)
mytext.remove()
fig.add_artist(mytext)
plt.savefig("text_after.png")        # text visible (correct)

Actual outcome

figimage_before.png

Image

figimage_after.png

Image

Expected outcome

figimage_before.png

Image

figimage_after.png

Image

Additional information

No response

Operating system

No response

Matplotlib Version

0.2.0.dev54524+unknown.ga94275af4

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions