Matplotlib EllipseSelector如何获取路径?

2024-09-30 22:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是matplotlib小部件EllipseSelector的基本示例。顾名思义,此小部件用于通过在轴上绘制椭圆来选择数据。
确切地说,用户可以通过单击并拖动轴来绘制和修改椭圆。每次释放鼠标按钮时,都会调用一个回调函数(例如:onselect)。
以下是一个例子:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import EllipseSelector

class EllipseExample:

    def __init__(self):

        # creating data points
        self.X, self.Y = (0, 1, 2), (0, -1, -2)
        self.XY = np.asarray((self.X, self.Y)).T

        # plotting
        self.fig, self.ax = plt.subplots()
        self.ax.scatter(self.X, self.Y) # just for visualization

        # creating the EllipseSelector
        self.es = EllipseSelector(self.ax, self.onselect,
                                  drawtype='box', interactive=True)

        # bool array about selection status of XY rows.
        self.selection_bool = None # e.g. (False, True, False)
        
        plt.show()

    # selector callback method
    def onselect(self, eclick, erelease):
        print('click: (%f, %f)' % (eclick.xdata, eclick.ydata))
        print('release  : (%f, %f)' % (erelease.xdata, erelease.ydata))
        # how to get the path of the selector's ellipse?
        # path = self.es.??? <--- no clue how to get there
        # self.selection_bool = path.contains_points(self.XY)
        # print('selection:\n', self.selection_bool)

example = EllipseExample()

我使用过其他matplotlib选择小部件(PolygonSelector、RectangleSelector、LassoSelector)。这些都以某种方式返回与选择形状相对应的选择顶点,可用于直接过滤数据(例如,矩形选择器给出矩形范围的x0、x1、y0、y1坐标),或创建路径并通过路径检查。如果数据在选择范围内,则包含_点。
基本上我是在问:
如何使用椭圆选择器不仅用于绘图和椭圆,而且用于选择器零件?如何获得绘制的椭圆的路径,以便我可以通过path.contains_points检查数据,如上面示例中的注释所示


Tags: 数据pathimportselfmatplotlib部件绘制plt
1条回答
网友
1楼 · 发布于 2024-09-30 22:22:55

似乎没有通过.contains_points()直接检查选择器中是否包含点的方法。我能找到的最简单的方法是从EllipseSelector的属性创建椭圆面片。这些属性是从矩形选择器中继承的。
通过将选择的中心、宽度和高度传递给matplotlib.patches.Ellipse,我们得到一个椭圆面片,我们可以在其上调用方法contains_points()。此方法返回一个布尔数组,每个元素对应一个数据点(True:选择包含点,False:选择不包含点)。
所述bool阵列可用于例如过滤数据帧。
警告:在任何情况下,都不能将此面片添加到轴上(即,不要绘制此面片),因为其坐标将被转换,并且如果没有转换步骤,您将无法再检查原始数据。
下面是一个逐步入门的友好示例,带有详细的代码注释:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import EllipseSelector
from matplotlib.patches import Ellipse

class EllipseSelectorExample:

    def __init__(self):

        # creating data points as numpy arrays
        self.X = np.asarray((0, 1, 2, 3, 4, 5, 6))
        self.Y = np.asarray((0, 0, 0, 0, 0, 0, 0))
        
        # plotting
        self.fig, self.ax = plt.subplots()
        self.ax.set_xlim(-1, 7), self.ax.set_ylim(-3, 3)
        self.ax.grid(True)
        self.ax.scatter(self.X, self.Y)

        # creating the EllipseSelector and connecting it to onselect
        self.es = EllipseSelector(self.ax, self.onselect,
                                  drawtype='box', interactive=True)
        plt.show()

    # selector callback method
    def onselect(self, eclick, erelease):

        # 1. Collect ellipse parameters (center, width, height)

        # getting the center property of the drawn ellipse
        cx, cy = self.es.center # tuple of floats: (x, y)

        # calculating the width and height
        # self.es.extents returns tuple of floats: (xmin, xmax, ymin, ymax)
        xmin, xmax, ymin, ymax = self.es.extents
        width = xmax - xmin
        height = ymax - ymin
        print(f'center=({cx:.2f},{cy:.2f}), '
              f'width={width:.2f}, height={height:.2f}')

        # 2. Create an ellipse patch
        # CAUTION: DO NOT PLOT (==add this patch to ax), as the coordinates will
        # be transformed and you will not be able to directly check your data
        # points.
        ellipse = Ellipse((cx,cy), width, height)

        # 3. Check which points are contained in the ellipse by directly calling
        # contains_points on the ellipse.
        # contains_points wants input like ( (x0,y0), (x1,y1), ... )

        # X=x0,x1,... Y=y0,y1,...  ->  [ [x0,y0], [x1,y1], [x2,y2], ... ]
        XY = np.asarray((self.X, self.Y)).T

        # calling contains_plot and returning our filter ndarray
        filter_array = ellipse.contains_points(XY)

        # 4. Apply filter to your data (optional)
        X_filtered = self.X[filter_array]
        Y_filtered = self.Y[filter_array]

        # results:
        print(f'\n'
              f'original data:\nX={self.X}\nY={self.Y}\n'
              f'filter_array={filter_array}\n'
              f'resulting data:\nX={X_filtered}\nY={Y_filtered}')

example = EllipseSelectorExample()

下面是上述示例的简短版本,检查点仅需3行代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import EllipseSelector
from matplotlib.patches import Ellipse

class EllipseSelectorExample:

    def __init__(self):
        self.MYDATA = np.array([[0, 1, 2, 3, 4, 5, 6],
                                [0, 0, 0, 0, 0, 0, 0]])
        self.fig, self.ax = plt.subplots()
        self.ax.set_xlim(-1, 7), self.ax.set_ylim(-3, 3), self.ax.grid(True)
        self.ax.scatter(self.MYDATA[0], self.MYDATA[1])
        self.es = EllipseSelector(self.ax, self.onselect,
                                  drawtype='box', interactive=True)
        plt.show()

    # selector callback method
    def onselect(self, eclick, erelease):
        ext = self.es.extents
        ellipse = Ellipse(self.es.center, ext[1]-ext[0], ext[3]-ext[2])
        # result:
        print(ellipse.contains_points(self.MYDATA.T))

example = EllipseSelectorExample()

相关问题 更多 >