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