【发布时间】:2021-09-14 15:29:44
【问题描述】:
下面是 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)。这些都以某种方式返回与选择形状对应的选择顶点,可用于直接过滤数据(例如 RectangleSelector 给出矩形范围的 x0、x1、y0、y1 坐标)或创建路径并通过 path.contains_points 进行检查如果数据在选择范围内。
基本上我在问:
如何使用 EllipseSelector 不仅用于绘图和椭圆,还用于选择器部分?如何获取绘制椭圆的路径,以便我可以通过path.contains_points检查我的数据,如上例中的 cmets 中所建议的那样。
【问题讨论】:
标签: python matplotlib widget