bqplot交互式图表:Selector选择器详解

bqplot交互式图表:Selector选择器详解

bqplot Plotting library for IPython/Jupyter notebooks bqplot 项目地址: https://gitcode.com/gh_mirrors/bq/bqplot

概述

bqplot是一个基于Python的交互式可视化库,它提供了丰富的图表类型和交互功能。在数据可视化中,交互式选择数据子集是一个常见需求,bqplot通过Selector(选择器)机制实现了这一功能。本文将详细介绍bqplot中各种Selector的使用方法和应用场景。

Selector基础

Selector是bqplot交互层的重要组成部分,它允许用户在图表上选择特定区域的数据点。bqplot提供了多种Selector类型,每种类型对应不同的选择方式:

  • 矩形区域选择:BrushSelector、FastIntervalSelector和MultiSelector
  • 最近点选择:IndexSelector
  • 自由绘制选择:LassoSelector

Selector需要与两个组件配合使用:

  1. 一个或多个Mark对象(如散点图、线图等),Selector会设置这些Mark的selected属性
  2. 一个或两个Scale对象(用于1D或2D选择),Selector的selected属性将表示为这些Scale的值

准备工作

在开始使用Selector前,我们需要准备一些示例数据:

import pandas as pd
import numpy as np
from bqplot import *
from bqplot.interacts import *

# 生成示例数据
price_data = pd.DataFrame(
    np.cumsum(np.random.randn(150, 2).dot([[0.5, 0.4], [0.4, 1.0]]), axis=0) + 100,
    columns=["Security 1", "Security 2"],
    index=pd.date_range(start="01-01-2007", periods=150),
)

dates_actual = price_data.index.values
prices = price_data["Security 1"].values

# 定义比例尺
scales = {"x": DateScale(), "y": LinearScale()}

BrushIntervalSelector(笔刷区间选择器)

BrushIntervalSelector允许用户通过绘制矩形区域来选择数据。

使用方法

  1. 点击并拖动创建新的选择区域
  2. 拖动选择区域的边缘可以调整宽度
  3. 拖动选择区域内部可以移动整个区域
  4. 在选择区域外点击并拖动会删除当前区域并创建新区域
scatter = Scatter(
    x=dates_actual,
    y=prices,
    scales=scales,
    colors=["orange"],
    selected_style={"opacity": "1"},
    unselected_style={"opacity": "0.2"},
)

brushintsel = BrushIntervalSelector(scale=scales["x"], marks=[scatter])

x_ax = Axis(label="Index", scale=scales["x"])
y_ax = Axis(label="Security 1 Price", scale=scales["y"], orientation="vertical")

fig = Figure(
    marks=[scatter],
    axes=[x_ax, y_ax],
    title="Brush Interval Selector示例",
    interaction=brushintsel,
)

垂直方向选择

通过设置orientation="vertical",可以实现垂直方向的选择:

create_figure(BrushIntervalSelector, orientation="vertical", scale=scales["y"])

BrushSelector(二维笔刷选择器)

BrushSelector是二维选择器,需要同时指定x和y比例尺:

create_figure(BrushSelector, x_scale=scales["x"], y_scale=scales["y"])

FastIntervalSelector(快速区间选择器)

FastIntervalSelector提供比BrushIntervalSelector更流畅的交互体验。

使用方法

  1. 第一次点击创建选择器
  2. 上下移动鼠标调整区间宽度
  3. 左右移动鼠标平移区间
  4. 后续点击可以冻结/解冻区间宽度
  5. 双击可以同时冻结宽度和平移
create_figure(FastIntervalSelector, scale=scales["x"])

LassoSelector(套索选择器)

LassoSelector允许用户通过自由绘制来选择数据点。

使用方法

  1. 点击并拖动绘制新的套索
  2. 点击套索可以选中/取消选中它
  3. 按Delete键删除选中的套索
create_figure(LassoSelector)

IndexSelector(索引选择器)

IndexSelector选择距离指定值最近的数据点。

使用方法

  1. 第一次点击创建并激活选择器
  2. 移动鼠标平移选择器
  3. 后续点击可以冻结/解冻选择器
create_figure(IndexSelector, scale=scales["x"])

MultiSelector(多重选择器)

MultiSelector相当于多个BrushSelector的组合。

使用方法

  1. 第一个笔刷工作方式与常规笔刷相同
  2. Ctrl+点击创建新笔刷
  3. 活动笔刷有绿色边框,非活动笔刷有红色边框
  4. Shift+点击取消当前活动笔刷
  5. Ctrl+Shift+点击清除所有笔刷
create_figure(MultiSelector, scale=scales["x"])

总结

bqplot的Selector机制为数据可视化提供了强大的交互能力。通过不同类型的Selector,用户可以根据需求选择最适合的数据选择方式。无论是简单的区间选择还是复杂的自由绘制选择,bqplot都能提供流畅的用户体验。掌握这些Selector的使用方法,可以大大增强数据探索和分析的效率。

bqplot Plotting library for IPython/Jupyter notebooks bqplot 项目地址: https://gitcode.com/gh_mirrors/bq/bqplot

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郁楠烈Hubert

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值