套索选区概述
套索选区是一种常见的对象选择方式,类似于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 plt
ax = 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 np
from matplotlib.widgets import LassoSelector
from matplotlib.path import Path
class SelectFromCollection:
"""
Select indices from a matplotlib collection using `LassoSelector`.
Selected indices are saved in the `ind` attribute. This tool fades out the
points that are not part of the selection (i.e., reduces their alpha
values). If your collection has alpha < 1, this tool will permanently
alter 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` subclass
Collection you want to select from.
alpha_other : 0 <= float <= 1
To highlight a selection, this tool sets all selected points to an
alpha value of 1 and non-selected points to *alpha_other*.
"""
def __init__(self, ax, collection, alpha_other=0.3):
self.canvas = ax.figure.canvas
self.collection = collection
self.alpha_other = alpha_other
self.xys = collection.get_offsets()
self.Npts = len(self.xys)
# Ensure that we have separate colors for each object
self.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_other
self.fc[self.ind, -1] = 1
self.collection.set_facecolors(self.fc)
self.canvas.draw_idle()
def disconnect(self):
self.lasso.disconnect_events()
self.fc[:, -1] = 1
self.collection.set_facecolors(self.fc)
self.canvas.draw_idle()
if __name__ == '__main__':
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.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()