如何清除以前绘制的Matplotlib文本框? [英] How can you clear a Matplotlib text box that was previously drawn?

查看:147
本文介绍了如何清除以前绘制的Matplotlib文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 matplotlib 中制作文本框.但是我看不到如何从渲染图中删除它们?画完文本框后好像没有figure.text.clear()或者figure.text(visible=False)?这是怎么做的?与传说不同的是,您似乎无法使它们可拖动?

I can make text boxes in matplotlib fine. But I dont see how to remove them from a rendered plot? There seems to be no figure.text.clear() or figure.text(visible=False) after you draw a text box? How is this done? and unlike legends, you seem to be unable to make them draggable?

推荐答案

文本框是艺术家.因此,如果您对它们保持引用,您应该可以对它们做很多事情.因此,在任何绘图代码中,而不是

Text boxes are artists. As such, you should be able to do lots of things with them if you keep a reference to them. Hence, in any plotting code, instead of

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

你可以做到

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

但是,如果您丢失了引用,则可以在 texts 属性中找到所有文本对象:

If you've lost the references, though, all the text objects can be found in the texts attribute:

fig.texts # is a list of Text objects

在 1.3.1 版中,执行 textvar.remove() 会生成 NotImplementedError(显然在 1.4 中已修复).但是,您可以通过将可见性设置为 False 来在一定程度上解决这个问题.

In version 1.3.1, doing textvar.remove() generates a NotImplementedError (apparently fixed in 1.4). However, you can get around that to some degree by setting the visibility to False.

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

将使您所有的文本框消失.

will make all your text boxes disappear.

这篇关于如何清除以前绘制的Matplotlib文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆