ChartMultiSelect API#
Bases: AnyWidget
Multi-region selection overlay for matplotlib charts.
Like ChartSelect but supports multiple persistent box/lasso selections,
each tagged with a class label. Draw regions for different classes, then
use get_labels to classify points.
Examples:
Basic usage:
import matplotlib.pyplot as plt
from wigglystuff import ChartMultiSelect
fig, ax = plt.subplots()
ax.scatter(x_data, y_data)
ms = ChartMultiSelect(fig, n_classes=3)
# ms.selections is a list of selection dicts
# ms.get_labels(x_data, y_data) returns class labels
Filtering by class:
Create a ChartMultiSelect widget from a matplotlib figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
A matplotlib figure to overlay selections on. |
required | |
n_classes
|
int
|
Number of class labels (1–4). |
2
|
mode
|
str
|
Selection mode ( |
'box'
|
modes
|
list[str] | None
|
Available modes. Defaults to |
None
|
selection_opacity
|
float
|
Opacity of selection fill (0–1). |
0.3
|
**kwargs
|
Any
|
Forwarded to |
{}
|
Source code in wigglystuff/chart_multi_select.py
clear #
from_callback
classmethod
#
from_callback(
draw_fn,
x_bounds: tuple[float, float],
y_bounds: tuple[float, float],
figsize: tuple[float, float] = (6, 6),
n_classes: int = 2,
mode: str = "box",
modes: list[str] | None = None,
**kwargs: Any
) -> "ChartMultiSelect"
Create a ChartMultiSelect that re-renders when selections change.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw_fn
|
|
required | |
x_bounds
|
tuple[float, float]
|
Fixed (min, max) for x-axis. |
required |
y_bounds
|
tuple[float, float]
|
Fixed (min, max) for y-axis. |
required |
figsize
|
tuple[float, float]
|
Figure size in inches. |
(6, 6)
|
n_classes
|
int
|
Number of class labels (1–4). |
2
|
mode
|
str
|
Initial selection mode. |
'box'
|
modes
|
list[str] | None
|
Available modes. |
None
|
**kwargs
|
Any
|
Passed to |
{}
|
Returns:
| Type | Description |
|---|---|
'ChartMultiSelect'
|
A |
Source code in wigglystuff/chart_multi_select.py
get_indices #
Indices of classified points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_arr
|
Array-like of x coordinates. |
required | |
y_arr
|
Array-like of y coordinates. |
required | |
class_id
|
Optional class filter (see |
None
|
Returns:
| Type | Description |
|---|---|
|
Numpy integer array of indices. |
Source code in wigglystuff/chart_multi_select.py
get_labels #
Return integer class labels for each point.
Points not covered by any selection get -1. When selections
overlap the last-drawn selection wins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_arr
|
Array-like of x coordinates (data space). |
required | |
y_arr
|
Array-like of y coordinates (data space). |
required |
Returns:
| Type | Description |
|---|---|
|
Numpy integer array of length |
Source code in wigglystuff/chart_multi_select.py
get_mask #
Boolean mask for classified points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_arr
|
Array-like of x coordinates. |
required | |
y_arr
|
Array-like of y coordinates. |
required | |
class_id
|
If |
None
|
Returns:
| Type | Description |
|---|---|
|
Boolean numpy array. |
Source code in wigglystuff/chart_multi_select.py
redraw #
Re-render the chart using the stored callback.
Only available for widgets created via from_callback().
Synced traitlets#
| Traitlet | Type | Notes |
|---|---|---|
mode |
str |
Selection mode: "box" or "lasso". |
modes |
list[str] |
Available modes (controls which buttons are shown). |
n_classes |
int |
Number of class labels (1–4). |
active_class |
int |
Currently active class for the next drawn selection. |
selections |
list[dict] |
All selections. Each dict has type, class_id, and geometry keys. |
selected_index |
int |
Index of the highlighted selection (-1 = none). |
x_bounds |
tuple[float, float] |
Min/max x-axis bounds from matplotlib. |
y_bounds |
tuple[float, float] |
Min/max y-axis bounds from matplotlib. |
axes_pixel_bounds |
tuple[float, float, float, float] |
Axes position in pixels (left, top, right, bottom). |
width |
int |
Canvas width in pixels. |
height |
int |
Canvas height in pixels. |
chart_base64 |
str |
Base64-encoded PNG of the matplotlib figure. |
selection_opacity |
float |
Opacity of selection fill (0-1). |
stroke_width |
int |
Width of selection border in pixels. |
Helper methods#
| Method | Returns | Description |
|---|---|---|
clear() |
None |
Remove all selections. |
get_labels(x_arr, y_arr) |
ndarray[int] |
Class labels per point (-1 = unclassified, last-drawn wins for overlap). |
get_mask(x_arr, y_arr, class_id=None) |
ndarray[bool] |
Boolean mask for classified points (optionally filtered by class). |
get_indices(x_arr, y_arr, class_id=None) |
ndarray[int] |
Indices of classified points. |
redraw() |
None |
Re-render chart (only for from_callback widgets). |