Matplotlib remove patches from figure

风流意气都作罢 提交于 2019-12-05 14:25:21

问题


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()

回答1:


Try this:

def reset(event):
    circle1.remove()

Also maybe you prefer:

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


来源:https://stackoverflow.com/questions/21687571/matplotlib-remove-patches-from-figure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!