Python Matplotlib从图中删除修补程序

Python Matplotlib从图中删除修补程序,python,matplotlib,interactive,Python,Matplotlib,Interactive,在我的例子中,我想在单击重置按钮时删除其中一个圆。但是,ax.clear()将清除当前图形上的所有圆圈 有人能告诉我如何只删除部分补丁吗 import matplotlib.patches as patches import matplotlib.pyplot as plt from matplotlib.widgets import Button fig = plt.figure() ax = fig.add_subplot(111) circle1 = patches.Circle((

在我的例子中,我想在单击重置按钮时删除其中一个圆。但是,ax.clear()将清除当前图形上的所有圆圈

有人能告诉我如何只删除部分补丁吗

import matplotlib.patches as patches
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

fig = plt.figure()
ax = fig.add_subplot(111) 

circle1 = patches.Circle((0.3, 0.3), 0.03, fc='r', alpha=0.5)
circle2 = patches.Circle((0.4, 0.3), 0.03, fc='r', alpha=0.5)
button = Button(plt.axes([0.8, 0.025, 0.1, 0.04]), 'Reset', color='g', hovercolor='0.975')
ax.add_patch(circle1)
ax.add_patch(circle2)

def reset(event):
    '''what to do here'''
    ax.clear()

button.on_clicked(reset)
plt.show()
试试这个:

def reset(event):
    circle1.remove()
也许你更喜欢:

def reset(event):
    circle1.set_visible(False)

这是不同的选择

ax.patches = []

它删除了所有的补丁。

我也尝试了答案1,虽然它在这种情况下工作,但在我自己的代码中不工作。工作原理是在将面片添加到轴后移除面片对象,而不是原始面片对象,如下所示:

circle1 = patches.Circle((0.3, 0.3), 0.03, fc='r', alpha=0.5)
circle2 = patches.Circle((0.4, 0.3), 0.03, fc='r', alpha=0.5)
button = Button(plt.axes([0.8, 0.025, 0.1, 0.04]), 'Reset', color='g', hovercolor='0.975')
c1=ax.add_patch(circle1)
c2=ax.add_patch(circle2)

def reset(event):
    c1.remove()

button.on_clicked(functools.partial(reset,patch=c1))
plt.show()

否则,我将收到NotImplementedError(“无法删除艺术家”)错误。

不知道为什么,但对我来说,补丁。删除()无法正常工作,而这项工作却非常有效。谢谢