【问题标题】:Matplotlib: How to clear a plot element using its handleMatplotlib:如何使用其句柄清除绘图元素
【发布时间】:2020-11-11 19:23:40
【问题描述】:

我不明白为什么这么难,但我不知道该怎么做。如果我绘制了多条线或点并想清除其中一条,那该怎么做?其他帖子指向remove(),但这不适用于我的情况。见例子:

Python 2.7.5 (default, Apr  2 2020, 13:16:51) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> 
>>> plt.ion()
>>> x = np.arange(100.) / 99
>>> y = np.sin(x)
>>> fig, ax = plt.subplots()
>>> h = ax.plot(x, y)
>>> h.remove()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: remove() takes exactly one argument (0 given)

【问题讨论】:

标签: python matplotlib


【解决方案1】:

我认为 JohanC 的回答确实是正确的。
您可以简单地尝试在h.remove() 行之前放置一个print(h)。我们会注意到一个列表对象被返回。但是,如果您使用 h = ax.plot(x, y) 代替然后使用 print(h),我们将看到只有一个像 Line2D 这样的对象被返回(不是列表)并且h.remove() 应该可以工作。

【讨论】:

  • 你的意思是说“h, = ax.plot(x,y)”,用逗号?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
相关资源
最近更新 更多