Matplotlib从figu删除修补程序

2024-05-26 20:45:08 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的例子中,我想在单击重置按钮时删除其中一个圆。但是,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()

Tags: importalphaaddmatplotlibasfigbuttonplt
1条回答
网友
1楼 · 发布于 2024-05-26 20:45:08

试试这个:

def reset(event):
    circle1.remove()

或许你更喜欢:

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

相关问题 更多 >

    热门问题