gpt4 book ai didi

python - “PathCollection”不可迭代 - 创建可拖动的散点图

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:17 24 4
gpt4 key购买 nike

我正在尝试创建一个带有可拖动标记的 Matplotlib 散点图。

我在 Matplotlib 站点上找到了一个可拖动矩形示例,https://matplotlib.org/users/event_handling.html 。该方法似乎是创建一个 DraggableRectangle 类来处理鼠标事件,并为条形图中的每个矩形进行初始化和连接。

我尝试用散点图做类似的事情,但是当我尝试迭代标记时,我得到一个 typeError: 'PathCollection' object is not iterable。

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

执行此操作的正确方法是什么?

最佳答案

如果其他人在寻找答案时遇到此问题,这里有一个基于 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)

关于python - “PathCollection”不可迭代 - 创建可拖动的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52840767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com