'PathCollection' 不可迭代 - 创建可拖动的散点图 [英] 'PathCollection' not iterable - creating a draggable scatter plot

查看:67
本文介绍了'PathCollection' 不可迭代 - 创建可拖动的散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a Matplotlib scatter plot with draggable markers.

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

I found a draggable rectangle example on the Matplotlib site, https://matplotlib.org/users/event_handling.html. The approach appears to be to create a DraggableRectangle class that handles the mouse events and which is initialized and connected for each rectangle in the bar plot.

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

I tried to do something similar with a scatter plot, but when I try to iterate through the markers I get a 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()

这样做的正确方法是什么?

What is the right way to do this?

推荐答案

如果其他任何人都在寻找答案,这是基于ImportanceOfBeingErnes注释中提供的链接的可拖动散点图.

In case anyone else comes across this in search of an answer, here is a draggable scatter based on the links provided in comments by 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)

这篇关于'PathCollection' 不可迭代 - 创建可拖动的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆