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

标签 python python-3.x matplotlib

我正在尝试创建一个带有可拖动标记的 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/

相关文章:

python - 使用索引时范围是否必须计算所有以前的值

python - 无法在 Windows 10 中下载 Django 2.0.7

python - Matplotlib,从 pandas 数据帧在 barplot 上绘制多个单独的 hline

python - 如何以Python方式将numpy.array传递给numpy.array中对象的方法

python - matplotlib savefig() 大小控制

python - 在多维列表上使用 index()

Python3 使用 exec 动态导入 - 为什么 'as' 不被执行?

python - 尝试使用 pip3.4 在 CentOS 5.11 服务器中安装 python 模块 (beautifulsoup) 时出错

python - 计算单词和字符串的频率

将 python 字典转换为 sqlite