Skip to content

[Bug]: Re-adding annotation texts to the axes causes them to be obscured #31748

@williamlus

Description

@williamlus

Bug summary

When annotation texts are removed and re-added to the axes, they become obscured in the second plot.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

linestyles = [
     ('Solid', 'solid'),      
     ('Dotted', 'dotted'),    
     ('Dashed', 'dashed'),    
     ('Dashdot', 'dashdot')]  

fig, ax = plt.subplots()

ax.set(ylim=(-0.5, len(linestyles)-0.5), yticks=np.arange(len(linestyles)), yticklabels=[name for name, _ in linestyles])

texts = []
for i, (name, linestyle) in enumerate(linestyles):
    text = ax.annotate(repr(linestyle), xy=(0.0, i), xycoords=ax.get_yaxis_transform(), xytext=(-6, -12), textcoords='offset points', ha='center', fontsize=12, color='blue')
    texts.append(text)

plt.savefig('plot1.png')

for text in texts: text.remove()
for text in texts: ax.add_artist(text)

plt.savefig('plot2.png')

Actual outcome

plot1.png

Image

plot2.png

Image

Expected outcome

plot1.png

Image

plot2.png

Image

Additional information

No response

Operating system

No response

Matplotlib Version

0.2.0.dev54528+unknown.gf4cf12592

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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