我正在尝试创建带有可拖动标记的Matplotlib散点图。
我在Matplotlib网站https://matplotlib.org/users/event_handling.html上找到了一个可拖动的矩形示例。该方法似乎是创建一个DraggableRectangle类,该类处理鼠标事件,并针对条形图中的每个矩形进行初始化和连接。
我试图用散点图做类似的事情,但是当我尝试遍历标记时,我遇到了typeError:'PathCollection'对象不可迭代。
import numpy as np
import matplotlib.pyplot as plt
class DraggableMarker:
def __init__(self, marker):
self.marker = marker
def connect(self):
self.cidpress = self.rect.figure.canvas.mpl_connect(
'button_press_event', self.on_press)
self.cidrelease = self.rect.figure.canvas.mpl_connect(
'button_release_event', self.on_release)
self.cidmotion = self.rect.figure.canvas.mpl_connect(
'motion_notify_event', self.on_motion)
def on_press(self, event):
pass
def on_motion(self, event):
pass
def on_release(self, event):
pass
def disconnect(self):
self.rect.figure.canvas.mpl_disconnect(self.cidpress)
self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
self.rect.figure.canvas.mpl_disconnect(self.cidmotion)
fig, ax = plt.subplots(1, 1)
markers = ax.scatter(np.random.rand(10), np.random.rand(10), marker ='o')
draggable_markers = []
for marker in markers:
draggable_marker = DraggableMarker(marker)
draggable_marker.connect()
draggable_markers.append(draggable_marker)
plt.show()
正确的方法是什么?
答案 0 :(得分:0)
万一有其他人在寻找答案,这是基于ImportanceOfBeingErnes注释中提供的链接的可拖动散点图。
import numpy as np
import matplotlib.pyplot as plt
class DraggableScatter():
epsilon = 5
def __init__(self, scatter):
self.scatter = scatter
self._ind = None
self.ax = scatter.axes
self.canvas = self.ax.figure.canvas
self.canvas.mpl_connect('button_press_event', self.button_press_callback)
self.canvas.mpl_connect('button_release_event', self.button_release_callback)
self.canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
plt.show()
def get_ind_under_point(self, event):
xy = np.asarray(self.scatter.get_offsets())
xyt = self.ax.transData.transform(xy)
xt, yt = xyt[:, 0], xyt[:, 1]
d = np.sqrt((xt - event.x)**2 + (yt - event.y)**2)
ind = d.argmin()
if d[ind] >= self.epsilon:
ind = None
return ind
def button_press_callback(self, event):
if event.inaxes is None:
return
if event.button != 1:
return
self._ind = self.get_ind_under_point(event)
def button_release_callback(self, event):
if event.button != 1:
return
self._ind = None
def motion_notify_callback(self, event):
if self._ind is None:
return
if event.inaxes is None:
return
if event.button != 1:
return
x, y = event.xdata, event.ydata
xy = np.asarray(self.scatter.get_offsets())
xy[self._ind] = np.array([x, y])
self.scatter.set_offsets(xy)
self.canvas.draw_idle()
fig, ax = plt.subplots(1, 1)
scatter = ax.scatter(np.random.rand(10), np.random.rand(10), marker='o')
DraggableScatter(scatter)