python - 面向对象访问matplotlib中的fill_between阴影区域

标签 python matplotlib plot fill

我正在尝试访问 matplotlib 图的阴影区域,这样我就可以在不执行 plt.cla() 的情况下将其删除 [因为 cla() 也清除包括轴标签在内的整个轴]

如果我正在绘制 I 线,我可以这样做:

import matplotlib.pyplot as plt
ax = plt.gca()
ax.plot(x,y)
ax.set_xlabel('My Label Here')

# then to remove the line, but not the axis label
ax.lines.pop()

但是,为了绘制一个区域,我执行:

ax.fill_between(x, 0, y)

所以 ax.lines 是空的。

请问如何清除这个阴影区域?

最佳答案

作为the documentation states fill_between 返回一个 PolyCollection 实例。集合存储在 ax.collections 中。所以

ax.collections.pop()

应该可以解决问题。

但是,我认为您必须小心删除正确的内容,以防 ax.linesax.collections 中有多个对象。您可以保存对该对象的引用,以便您知道要删除哪个对象:

fill_between_col = ax.fill_between(x, 0, y)

然后删除:

ax.collections.remove(fill_between_col)

编辑:还有另一种方法,可能是最好的方法:所有艺术家都有一种称为 remove 的方法。这正是你想要的:

fill_between_col.remove()

关于python - 面向对象访问matplotlib中的fill_between阴影区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30847734/

相关文章:

matlab - 计算测量数据的正切值

matlab - 在相空间人像matlab中添加箭头

以倍频程绘制决策边界

python - 我无法使用pip安装opencv-contrib-python

python - 绘制人口稀少的二维 numpy 数组

python - 在没有 root 权限的情况下安装 matplotlib 及其依赖项

python - matplotlib 的 'webagg' 后端可以与我的 Django 站点一起使用吗?

拦截函数调用的 Pythonic 方式?

python - (Python) 类型错误 : 'float' object is not subscriptable

python - 应更改哪些属性以允许通过 Selenium 上传图像