套索概述
套索(Lasso)是與套索選區(LassoSelector)相似的matplotlib部件(widgets),兩者的區別主要在于:
繼承關系:
- 套索具體實現定義為matplotlib.widgets.Lasso類,繼承關系為:Widget->AxesWidget->Lasso。
- 套索選區具體實現定義為matplotlib.widgets.LassoSelector類,繼承關系為:Widget->AxesWidget->_SelectorWidget->LassoSelector。
構造參數:
- Lasso類的簽名為class matplotlib.widgets.Lasso(ax, xy, callback=None, useblit=True),Lasso類需要給定套索一個起始的坐標。
- LassoSelector類的簽名為class matplotlib.widgets.LassoSelector(ax, onselect=None, useblit=True, lineprops=None, button=None)。
事件處理:
- Lasso事件在鼠標釋放時即被銷毀。
- LassoSelector在鼠標釋放時仍然可以繼續與子圖交互,直到斷開與子圖的連接。
Lasso類構造函數的參數為:
- ax:套索生效的子圖,類型為matplotlib.axes.Axes的實例。
- xy:套索起始的坐標。
- callback:套索完成即鼠標釋放時執行的回調函數,函數簽名為def func(verts),verts的為套索端點的坐標列表。
套索可以使用matplotlib.path.Path類的contains_point方法獲取選區內的數據點。
貌似 Lasso是實驗性API,還不夠完善,matplotlib 3.3之后可能逐步廢棄 Lasso。
案例:
官方案例,https://matplotlib.org/3.2.1/gallery/event_handling/lasso_demo.html
案例說明
案例代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
from matplotlib import colors as mcolors, path from matplotlib.collections import RegularPolyCollection import matplotlib.pyplot as plt from matplotlib.widgets import Lasso import numpy as np class Datum: colorin = mcolors.to_rgba( "red" ) colorout = mcolors.to_rgba( "blue" ) def __init__( self , x, y, include = False ): self .x = x self .y = y if include: self .color = self .colorin else : self .color = self .colorout class LassoManager: def __init__( self , ax, data): self .axes = ax self .canvas = ax.figure.canvas self .data = data self .Nxy = len (data) facecolors = [d.color for d in data] self .xys = [(d.x, d.y) for d in data] self .collection = RegularPolyCollection( 6 , sizes = ( 100 ,), facecolors = facecolors, offsets = self .xys, transOffset = ax.transData) ax.add_collection( self .collection) self .cid = self .canvas.mpl_connect( 'button_press_event' , self .onpress) def callback( self , verts): facecolors = self .collection.get_facecolors() p = path.Path(verts) ind = p.contains_points( self .xys) for i in range ( len ( self .xys)): if ind[i]: facecolors[i] = Datum.colorin else : facecolors[i] = Datum.colorout self .canvas.draw_idle() self .canvas.widgetlock.release( self .lasso) del self .lasso def onpress( self , event): if self .canvas.widgetlock.locked(): return if event.inaxes is None : return self .lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self .callback) # acquire a lock on the widget drawing self .canvas.widgetlock( self .lasso) if __name__ = = '__main__' : np.random.seed( 19680801 ) data = [Datum( * xy) for xy in np.random.rand( 100 , 2 )] ax = plt.axes(xlim = ( 0 , 1 ), ylim = ( 0 , 1 ), autoscale_on = False ) ax.set_title( 'Lasso points using left mouse button' ) lman = LassoManager(ax, data) plt.show() |
代碼分析
案例的關鍵代碼在于LassoManager類的onpress方法和callback方法。由于Lasso類在事件處理上比較原始,需要用戶進行控制,在鼠標按下、釋放事件中需要使用canvas.widgetlock對象鎖定/解鎖繪圖功能,保證只有一個對象進行繪圖,canvas.widgetlock是matplotlib.widgets.LockDraw類的實例。
總結
盡量使用套索選區(LassoSelector)而不是套索(Lasso),兩者功能相似,索選區(LassoSelector)使用相對更簡單一些,套索(Lasso)還有一些BUG,matplotlib 3.3已不再推薦使用。
到此這篇關于matplotlib部件之套索Lasso的使用的文章就介紹到這了,更多相關matplotlib 套索內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/mighty13/article/details/113593589