套索選區概述
套索選區是一種常見的對象選擇方式,類似于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
方法擷取選區内的資料點。
案例:套索選取基本實作
官方案例一,套索選區基本實作。
案例說明
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2csoXRUVGMoJjWwFjMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL0YDNzATOxkTM5ITMwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
單擊滑鼠拖動到适當位置釋放滑鼠形成套索選區,控制台輸出選區的端點坐标清單。
控制台輸出:
[(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()