小编Ide*_*ist的帖子

Matplotlib从图中删除补丁

在我的情况下,我想在单击重置按钮时删除其中一个圆圈.但是,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()
Run Code Online (Sandbox Code Playgroud)

python interactive matplotlib

16
推荐指数
2
解决办法
1万
查看次数

Matplotlib以交互方式拖动重叠点

在我的情况下,我只想每次拖动一个点.但是,由于两个点重叠很重,拖动一个点会导致拖动另一个点.我怎么才能拖动上面的点?谢谢!

from pylab import *
from scipy import *
import matplotlib.pyplot as plt
import matplotlib.patches as patches


class DraggablePoint:
    def __init__(self, p):
        self.point = p
        self.press = None

    def connect(self):
        self.cidpress = self.point.figure.canvas.mpl_connect('button_press_event', self.button_press_event)
        self.cidrelease = self.point.figure.canvas.mpl_connect('button_release_event', self.button_release_event)
        self.cidmotion = self.point.figure.canvas.mpl_connect('motion_notify_event', self.motion_notify_event)

    def disconnect(self):
    'disconnect all the stored connection ids'
        self.point.figure.canvas.mpl_disconnect(self.cidpress)
        self.point.figure.canvas.mpl_disconnect(self.cidrelease)
        self.point.figure.canvas.mpl_disconnect(self.cidmotion)


    def button_press_event(self,event):
        if event.inaxes != self.point.axes:
            return
        contains = self.point.contains(event)[0]
        if not contains: return
        self.press = self.point.center, event.xdata, event.ydata


    def button_release_event(self,event):
        self.press = None
        self.point.figure.canvas.draw()

    def motion_notify_event(self, …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

6
推荐指数
2
解决办法
4030
查看次数

标签 统计

matplotlib ×2

python ×2

interactive ×1