Is it possible to get a list of current annotations and to delete the ones I don't want?

Hi there,

I’ve been adding annotations to a figure using fig.add_annotations(...) . However I want to be able to access the current annotations of a figure object and delete the ones I don’t want.

Many thanks.

Here is an example on how to create annotations. Deleting them would be just deleting the corresponding list entry.

A figure’s annotations are stored in a tuple in fig.layout.annotations.
I don’t know about deleting them, but say you want to hide the i_th annotation :

fig.layout.annotations[i].visible = False

Found from the doc here : Layout.annotations in Python

The plot annotations are stored in the figure layout as a dictionary. To manipulate them, you need to determine which annotation belongs to which figure plot (if you have subplots). Also, this can be directly accessed as the class member my_plot.figure.layout.annotations[i]

This structure, in type definition is:
Dict[Tuple[Tuple[AnnotationObject], ...]]

>>> my_plot.figure.layout["annotations"] 
(layout.Annotation({
     'align': 'left',
     'arrowhead': 0,
     'showarrow': True,
     'text': 't=2.9<br>value:2.18<br>',
     'x': 2.93008,
     'xref': 'x',
     'y': 2.1790664113761644,
     'yref': 'y'
 }),
 layout.Annotation({
     'align': 'left',
     'arrowhead': 0,
     'showarrow': True,
     'text': 't=3.6<br>value:2.05<br>',
     'x': 3.623286,
     'xref': 'x',
     'y': 2.049381178508681,
     'yref': 'y'
 }))

Since the annotations object is a tuple, you cannot remove individual elements; the tuple has to be replaced. To remove them all, just replace the entire object:

my_plot.figure.layout["annotations"] = tuple()

If you’re simple interested in hiding an individual element, it’s easier to just use:

my_plot.figure.layout.annotations[i].visible = False