matplotlib部件(widgets)之套索选区(LassoSelector)

本文介绍matplotlib中的套索选区功能,通过matplotlib.widgets.LassoSelector类实现自由手绘选择子图中的数据点,并提供两个案例演示如何使用此功能。

套索选区概述

套索选区是一种常见的对象选择方式,类似于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()
<think>我们正在讨论matplotlib.widgets模块。根据引用[4],我们知道Jupyter Widgets可以用于创建交互式可视化,而matplotlib.widgetsMatplotlib自带的交互式控件模块,它允许在图形中添加滑块、按钮等控件,并实现用户交互。 引用[1]提到,指令式绘图(pyplot)适合快速可视化但不适合交互式GUI应用,而widgets模块正是为了增强交互性。 引用[3]介绍了Matplotlib的基本绘图结构,包括创建坐标轴和图形,这在使用widgets时也是基础。 因此,我们将重点介绍matplotlib.widgets模块的功能和使用方法。 功能概述: matplotlib.widgets模块提供了一系列控件,如按钮(Button)、单选按钮(RadioButtons)、复选框(CheckButtons)、滑块(Slider)、文本框(TextBox)等,这些控件可以嵌入到Matplotlib图形中,并绑定回调函数,实现交互式数据可视化。 使用步骤: 1. 导入必要的模块。 2. 创建图形和坐标轴,注意为控件预留位置(通常使用subplots_adjust或GridSpec调整布局)。 3. 创建控件,并指定其位置和属性。 4. 为控件绑定回调函数(事件处理函数)。 5. 显示图形。 示例:使用滑块控件(Slider)调整正弦曲线的频率和振幅。 下面我们通过一个具体示例来演示如何使用matplotlib.widgets.Slider。 注意:在Jupyter Notebook中运行交互式控件需要适当的环境(如使用%matplotlib widget或%matplotlib notebook),或者使用独立的GUI后端(如Tkinter, Qt等)。 我们将创建一个包含两个滑块(分别控制频率和振幅)的图形,滑块的值改变时,正弦曲线会实时更新。 步骤详解:</think>### Matplotlib.widgets 使用指南 `matplotlib.widgets` 是 Matplotlib 提供的交互式控件模块,允许在图表中嵌入可交互元素(如按钮、滑块等),实现动态数据可视化。以下是核心功能介绍和使用方法: #### 一、核心控件类型 1. **按钮控件** - `Button`:执行单次操作的按钮 - `RadioButtons`:单选按钮组 - `CheckButtons`:多选按钮组 ```python from matplotlib.widgets import Button btn_ax = plt.axes([0.7, 0.05, 0.1, 0.04]) # 位置参数 [左, 下, 宽, 高] button = Button(btn_ax, '重置') ``` 2. **滑块控件** - `Slider`:连续值调节滑块 - `RangeSlider`:范围选择滑块 ```python from matplotlib.widgets import Slider slider_ax = plt.axes([0.2, 0.1, 0.6, 0.03]) slider = Slider(slider_ax, '振幅', 0.1, 5.0, valinit=1.0) ``` 3. **文本输入控件** `TextBox`:接收用户文本输入 ```python from matplotlib.widgets import TextBox text_ax = plt.axes([0.2, 0.05, 0.2, 0.04]) text_box = TextBox(text_ax, '公式:', initial="sin(x)") ``` 4. **其他高级控件** `Cursor`(十字准线)、`PolygonSelector`(多边形选择器)等 #### 二、核心交互机制 1. **回调函数绑定** 所有控件通过事件回调实现交互: ```python def update(val): amp = slider.val line.set_ydata(amp * np.sin(x)) fig.canvas.draw_idle() # 重绘图形 slider.on_changed(update) # 绑定滑块值变化事件 ``` 2. **控件布局管理** 使用 `plt.axes()` 指定控件位置(归一化坐标): ```python plt.subplots_adjust(bottom=0.25) # 为底部控件预留空间 ``` #### 三、完整使用示例 ```python import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button # 创建数据 x = np.linspace(0, 2*np.pi, 200) y = np.sin(x) # 初始化图形 fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25) # 底部预留控件空间 line, = ax.plot(x, y) # 添加滑块 slider_ax = plt.axes([0.2, 0.1, 0.6, 0.03]) slider = Slider(slider_ax, '频率', 0.1, 5.0, valinit=1.0) # 添加按钮 btn_ax = plt.axes([0.8, 0.025, 0.1, 0.04]) button = Button(btn_ax, '重置') # 回调函数 def update(val): freq = slider.val line.set_ydata(np.sin(freq * x)) fig.canvas.draw_idle() def reset(event): slider.reset() # 绑定事件 slider.on_changed(update) button.on_clicked(reset) plt.show() ``` 此代码创建正弦波图形,用户可通过滑块调整频率,点击按钮重置参数[^3]。 #### 四、应用场景 1. **参数动态调节**:实时调整模型参数(如滤波器截止频率) 2. **数据探索**:交互式浏览大型数据集 3. **教学演示**:动态展示数学函数变化 4. **仪表盘开发**:结合其他控件构建分析面板 > **注意**:在 Jupyter 环境中运行时,需先执行 `%matplotlib widget` 启用交互支持[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值