套索选区概述

套索选区是一种常见的对象选择方式,类似于Photoshop中的套索功能,在一个子图中,根据拖动鼠标的轨迹形成选区。在matplotlib中的套索选区属于部件(widgets),matplotlib中的部件都是中性(neutral )的,即与具体后端实现无关。

套索选区具体实现定义为matplotlib.widgets.LassoSelector类,继承关系为:Widget->AxesWidget->_SelectorWidget->LassoSelector

LassoSelector类的签名为class matplotlib.widgets.LassoSelector(ax, onselect=None, useblit=True, lineprops=None, button=None)

LassoSelector类构造函数的参数为:

  • ax:套索选区生效的子图,类型为matplotlib.axes.Axes的实例。
  • onselect:套索选区完成即鼠标释放时执行的回调函数,函数签名为def func(verts)verts的为套索端点的坐标列表。
  • lineprops:套索选区的线条属性,默认值为None
  • button:设置可用于触发选区的鼠标键,MouseButton列表,默认为所有鼠标键。

套索选区可以使用matplotlib.path.Path类的contains_point方法获取选区内的数据点

案例:套索选取基本实现

官方案例一,套索选区基本实现。

案例说明

单击鼠标拖动到适当位置释放鼠标形成套索选区,控制台输出选区的端点坐标列表。
控制台输出:

[(0.25326881720430117, 1.4168308702791461), (0.25326881720430117, 1.4113574165298304), (0.25326881720430117, 1.4004105090311987), (0.25326881720430117, 1.3949370552818827), (0.25326881720430117, 1.3839901477832512), (0.2570537634408603, 1.3730432402846195), (0.2570537634408603, 1.3675697865353036), (0.2608387096774194, 1.3566228790366721), (0.2684086021505377, 1.3456759715380404), (0.27219354838709686, 1.329255610290093), (0.275978494623656, 1.3128352490421455), (0.2797634408602151, 1.3073617952928296), (0.28733333333333344, 1.2964148877941981), (0.28733333333333344, 1.2909414340448824), (0.2986881720430108, 1.2799945265462507), (0.30247311827957, 1.2745210727969347), (0.30625806451612914, 1.269047619047619), (0.31004301075268825, 1.2635741652983032), (0.31382795698924737, 1.2581007115489875), (0.3213978494623657, 1.2526272577996715), (0.32518279569892483, 1.2471538040503558), (0.33275268817204307, 1.2416803503010398), (0.3365376344086022, 1.2416803503010398), (0.3403225806451614, 1.236206896551724), (0.35167741935483876, 1.2252599890530924), (0.3554623655913979, 1.2252599890530924), (0.3592473118279571, 1.2252599890530924), (0.36681720430107534, 1.2197865353037767), (0.37060215053763446, 1.2143130815544607), (0.3743870967741937, 1.2143130815544607), (0.3781720430107528, 1.208839627805145), (0.3819569892473119, 1.208839627805145), (0.38574193548387103, 1.208839627805145), (0.38952688172043015, 1.2033661740558292), (0.3933118279569894, 1.2033661740558292), (0.3970967741935485, 1.2033661740558292), (0.40466666666666673, 1.2033661740558292), (0.40845161290322585, 1.2033661740558292), (0.4160215053763442, 1.208839627805145), (0.4235913978494624, 1.208839627805145), (0.43116129032258077, 1.2143130815544607), (0.4349462365591399, 1.2197865353037767), (0.4425161290322581, 1.2197865353037767), (0.45008602150537647, 1.236206896551724), (0.4576559139784947, 1.2416803503010398), (0.4614408602150538, 1.2471538040503558), (0.46522580645161304, 1.2581007115489875), (0.4727956989247313, 1.2581007115489875), (0.4727956989247313, 1.269047619047619), (0.4765806451612904, 1.269047619047619), (0.4765806451612904, 1.2745210727969347), (0.4803655913978495, 1.2745210727969347), (0.4803655913978495, 1.2854679802955664), (0.48415053763440874, 1.2854679802955664), (0.48415053763440874, 1.2909414340448824), (0.48415053763440874, 1.2964148877941981), (0.48793548387096786, 1.3018883415435138), (0.48793548387096786, 1.3073617952928296), (0.491720430107527, 1.3183087027914615), (0.491720430107527, 1.3237821565407772), (0.491720430107527, 1.329255610290093), (0.491720430107527, 1.3347290640394087), (0.491720430107527, 1.3402025177887247), (0.491720430107527, 1.3456759715380404), (0.491720430107527, 1.3511494252873564), (0.491720430107527, 1.3566228790366721), (0.491720430107527, 1.3620963327859879), (0.491720430107527, 1.3675697865353036), (0.491720430107527, 1.3730432402846195), (0.491720430107527, 1.3839901477832512), (0.491720430107527, 1.389463601532567), (0.491720430107527, 1.4004105090311987), (0.491720430107527, 1.4058839627805144), (0.491720430107527, 1.4113574165298304), (0.491720430107527, 1.4168308702791461), (0.48793548387096786, 1.4223043240284619), (0.48415053763440874, 1.4277777777777776), (0.48415053763440874, 1.4332512315270935), (0.48415053763440874, 1.4387246852764095), (0.48415053763440874, 1.4441981390257252), (0.4803655913978495, 1.449671592775041), (0.4803655913978495, 1.4551450465243567), (0.4803655913978495, 1.4606185002736727), (0.4803655913978495, 1.4660919540229884), (0.4803655913978495, 1.4715654077723044), (0.4803655913978495, 1.4770388615216201), (0.4765806451612904, 1.4825123152709359),
(0.4765806451612904, 1.4879857690202516), (0.4727956989247313, 1.4934592227695676), (0.4727956989247313, 1.4989326765188835), (0.4727956989247313, 1.5044061302681992), (0.4727956989247313, 1.509879584017515), (0.46901075268817216, 1.5153530377668307), (0.46901075268817216, 1.5153530377668307)]

案例代码

from matplotlib.widgets import LassoSelector
import matplotlib.pyplot as pltax = plt.subplot(111)
ax.plot([1,2])def onselect(verts):print(verts)lasso = LassoSelector(ax, onselect)
plt.show()

案例:

官方案例,https://matplotlib.org/gallery/widgets/Lasso_selector.html

案例说明


上图为套索区域,下图为套索选中的数据点。

案例代码

import numpy as npfrom matplotlib.widgets import LassoSelector
from matplotlib.path import Pathclass SelectFromCollection:"""Select indices from a matplotlib collection using `LassoSelector`.Selected indices are saved in the `ind` attribute. This tool fades out thepoints that are not part of the selection (i.e., reduces their alphavalues). If your collection has alpha < 1, this tool will permanentlyalter the alpha values.Note that this tool selects collection objects based on their *origins*(i.e., `offsets`).Parameters----------ax : `~matplotlib.axes.Axes`Axes to interact with.collection : `matplotlib.collections.Collection` subclassCollection you want to select from.alpha_other : 0 <= float <= 1To highlight a selection, this tool sets all selected points to analpha value of 1 and non-selected points to *alpha_other*."""def __init__(self, ax, collection, alpha_other=0.3):self.canvas = ax.figure.canvasself.collection = collectionself.alpha_other = alpha_otherself.xys = collection.get_offsets()self.Npts = len(self.xys)# Ensure that we have separate colors for each objectself.fc = collection.get_facecolors()if len(self.fc) == 0:raise ValueError('Collection must have a facecolor')elif len(self.fc) == 1:self.fc = np.tile(self.fc, (self.Npts, 1))self.lasso = LassoSelector(ax, onselect=self.onselect)self.ind = []def onselect(self, verts):path = Path(verts)self.ind = np.nonzero(path.contains_points(self.xys))[0]self.fc[:, -1] = self.alpha_otherself.fc[self.ind, -1] = 1self.collection.set_facecolors(self.fc)self.canvas.draw_idle()def disconnect(self):self.lasso.disconnect_events()self.fc[:, -1] = 1self.collection.set_facecolors(self.fc)self.canvas.draw_idle()if __name__ == '__main__':import matplotlib.pyplot as plt# Fixing random state for reproducibilitynp.random.seed(19680801)data = np.random.rand(100, 2)subplot_kw = dict(xlim=(0, 1), ylim=(0, 1), autoscale_on=False)fig, ax = plt.subplots(subplot_kw=subplot_kw)pts = ax.scatter(data[:, 0], data[:, 1], s=80)selector = SelectFromCollection(ax, pts)def accept(event):if event.key == "enter":print("Selected points:")print(selector.xys[selector.ind])selector.disconnect()ax.set_title("")fig.canvas.draw()fig.canvas.mpl_connect("key_press_event", accept)ax.set_title("Press enter to accept selected points.")plt.show()

matplotlib部件(widgets)之套索选区(LassoSelector)相关推荐

  1. matplotlib部件(widgets)之套索(Lasso)

    套索概述 套索(Lasso)是与套索选区(LassoSelector)相似的matplotlib部件(widgets),两者的区别主要在于: 继承关系: 套索具体实现定义为matplotlib.wid ...

  2. matplotlib部件(widgets)之矩形选区(RectangleSelector)

    矩形选区概述 矩形选区是一种常见的对象选择方式,这个名词最常见于Photoshop中,用于在一个子图选择鼠标拖动的矩形区域中的元素,在matplotlib中的矩形选区属于部件(widgets),mat ...

  3. PS 套索选区工具(2) 多边形套索工具 磁性套索工具使用技巧

    上文PS 套索选区工具(1) 套索工具基础使用带大家了解了套索工具的基本操作 这边 我们右键套索工具 它还有个 多边形套索工具 多边形套索工具是用来画直线的 我们选中它 然后 我们鼠标点击一下 然后 ...

  4. matplotlib部件(widgets)之多边形选区(PolygonSelector)

    多边形选区概述 多边形选区是一种常见的对象选择方式,在一个子图中,单击鼠标左键即构建一个多边形的端点,最后一个端点与第一个端点重合即完成多边形选区,选区即为多个端点构成的多边形.在matplotlib ...

  5. matplotlib部件(widgets)之范围选区(SpanSelector)

    范围选区概述 范围选区是一种常见的对象选择方式,在一个子图中,可以在某一个轴方向上用鼠标选择起始范围的数据,这个特性可用来实现数据缩放(datazoom).在matplotlib中的范围选区属于部件( ...

  6. matplotlib部件(widgets)之椭圆形选区(EllipseSelector)

    椭圆形选区概述 椭圆形选区是一种常见的对象选择方式,用于在一个子图选择鼠标拖动的椭圆形区域中的元素,在matplotlib中的椭圆形选区属于部件(widgets),matplotlib中的部件都是中性 ...

  7. PS 套索选区工具(1) 套索工具基础使用

    套索工具和之前的几个一样 也是用来做选区的 我们先打开ps 那么 我这边已经打开了一个视图 我们在屏幕左侧这个地方找到 套索工具 右键它 这边有三个操作工具 上一文中 我们学的矩形选框工具 在图形上是 ...

  8. php widgets,Yii框架小部件(Widgets)用法实例详解

    目录 小部件 使用小部件 配置全局默认值 创建小部件 最佳实践 本文实例讲述了Yii框架小部件(Widgets)用法.分享给大家供大家参考,具体如下: 小部件 小部件是在 视图 中使用的可重用单元, ...

  9. java拖拽选区_matplotlib之多边形选区(PolygonSelector)的使用

    多边形选区概述 多边形选区是一种常见的对象选择方式,在一个子图中,单击鼠标左键即构建一个多边形的端点,最后一个端点与第一个端点重合即完成多边形选区,选区即为多个端点构成的多边形.在matplotlib ...

最新文章

  1. php转换文字Unicode,php实现将中文转为unicode的方法
  2. 有没搞错?Java 对象不使用时要赋值为 null?
  3. 第十章 基本数据结构——栈和队列
  4. BH38旋转编码器初步测试
  5. python和java哪个-Python和Java哪个好?有什么区别?
  6. 神策 2021 数据驱动大会丨北京主会场首日直播,拼团早鸟票特惠来袭
  7. Verdaccio介绍及安装 -- nodejs私有npm proxy registry代理
  8. 使用Delphi7构建工商银行电子口令密码查找助手
  9. Java 泛型 extends T 和 super T 详解
  10. android UI 标签
  11. linux中config文件怎么打开,linux-如何使用CoreOS的cloud-config文件启动Dock...
  12. maven配置tomcat版本
  13. imagenet2012 label
  14. c#中PROCESS的用法
  15. 苹果系统虚拟机无usb服务器,win10系统苹果电脑运行虚拟机后无法识别显示U盘的详细方案...
  16. 生于安乐,死于忧患.
  17. Win10《芒果TV》更新v3.8.30流星版:优化稳定性、升级无边框播放体验
  18. plugins 插件
  19. java ppt控件_Java版PPT操作控件Spire.Presentation v2.12.2新版来袭!支持获取具有超链接的目标幻灯片...
  20. AC-DC电源管理芯片选型及应用

热门文章

  1. 长江雨课堂习题答案获取
  2. zblog2.X程序卢松松模版独立留言本最新完美设置!
  3. 2021年全球飞机维护、维修和运行收入大约93440百万美元,预计2028年达到116920百万美元,2022至2028期间,年复合增长率CAGR为3.5%
  4. 数据透视表函数综合作用
  5. lol服务器维护补偿,LOL服务器延长维护,官方给出2个补偿,老玩家笑了:就这?...
  6. SAP将“在中国,为中国”贯彻到底!
  7. hihoCoder 无间道之并查集
  8. 电脑硬件知识入门之CPU篇
  9. CAS(中央认证服务中心)
  10. 机械自动化Pk计算机专业,机械与自动化专业主要学什么 就业方向有哪些