Matplotlib从图中删除补丁 [英] Matplotlib remove patches from figure

查看:136
本文介绍了Matplotlib从图中删除补丁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就我而言,我想在单击重置按钮时删除一个圆圈.但是,ax.clear()会清除当前图形上的所有圆圈.

In my case, I want to remove one of the circle when clicking reset button. However, ax.clear() would clear all circles on the current figure.

有人可以告诉我如何仅删除部分补丁吗?

Can someone tell me how to remove only part of the patches?

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)

这篇关于Matplotlib从图中删除补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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