How can you clear the Matplotlib text box that was previously drawn?

I can do the text fields in matplotlib in order. But I don’t see how to remove them from the visualized plot? It seems that after drawing the text field there is no figure.text.clear () or figure.text (visible = False)? How it's done? and unlike legends, you can't seem to get them to drag and drop?

+6
source share
1 answer

Text fields are artists. This way you can do many things with them if you keep a link to them . Therefore, in any graphic code, instead of

fig.text(0, 0, 'My text') 

You can do

 textvar = fig.text(0, 0, 'My text') 

If you have lost links, all text objects can be found in the texts attribute:

 fig.texts # is a list of Text objects 

In version 1.3.1, executing textvar.remove() throws a NotImplementedError (obviously fixed in 1.4). However, you can get around this to some extent by setting the visibility to False.

 for txt in fig.texts: txt.set_visible(False) 

will cause all text fields to disappear.

+11
source

Source: https://habr.com/ru/post/975743/


All Articles