ChartSelect API#
Bases: AnyWidget
Region selection overlay for matplotlib charts.
Allows interactive box or lasso (freehand) selection on a static matplotlib chart. Returns selection coordinates in data space for user-side filtering.
Examples:
Basic usage:
import matplotlib.pyplot as plt
from wigglystuff import ChartSelect
fig, ax = plt.subplots()
ax.scatter(x_data, y_data)
select = ChartSelect(fig)
# select.selection contains the selection bounds/vertices
# select.has_selection is True when a selection exists
Filtering with a mask:
Filtering a DataFrame:
Create a ChartSelect widget from a matplotlib figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
A matplotlib figure to overlay selection on. |
required | |
mode
|
str
|
Selection mode ("box" or "lasso"). |
'box'
|
modes
|
list[str] | None
|
List of available modes. Defaults to ["box", "lasso"]. Pass a single-item list to lock to one mode. |
None
|
selection_color
|
str
|
Fill color for selection region. |
'#3b82f6'
|
selection_opacity
|
float
|
Opacity of selection fill (0-1). |
0.3
|
**kwargs
|
Any
|
Forwarded to |
{}
|
Source code in wigglystuff/chart_select.py
clear #
contains_point #
Check if a point is inside the selection region.
Uses matplotlib's Path.contains_point for lasso selections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
X coordinate in data space. |
required |
y
|
float
|
Y coordinate in data space. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the point is inside the selection, False otherwise. |
Source code in wigglystuff/chart_select.py
from_callback
classmethod
#
from_callback(
draw_fn,
x_bounds: tuple[float, float],
y_bounds: tuple[float, float],
figsize: tuple[float, float] = (6, 6),
mode: str = "box",
modes: list[str] | None = None,
**kwargs: Any
) -> "ChartSelect"
Create a ChartSelect that auto-updates when selection changes.
The callback function is called on init and whenever the selection
changes. Use redraw() to manually trigger a re-render.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw_fn
|
A function(ax, widget) that draws onto the axes. Receives the axes and the widget instance, allowing access to widget.selection and widget.has_selection. The axes is pre-cleared and bounds are pre-set. |
required | |
x_bounds
|
tuple[float, float]
|
(min, max) for x-axis - fixed for lifetime of widget. |
required |
y_bounds
|
tuple[float, float]
|
(min, max) for y-axis - fixed for lifetime of widget. |
required |
figsize
|
tuple[float, float]
|
Figure size in inches. |
(6, 6)
|
mode
|
str
|
Selection mode ("box" or "lasso"). |
'box'
|
modes
|
list[str] | None
|
List of available modes. Defaults to ["box", "lasso"]. |
None
|
**kwargs
|
Any
|
Passed to ChartSelect (selection_color, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
'ChartSelect'
|
A ChartSelect instance with auto-update behavior and redraw() method. |
Examples:
def draw_chart(ax, widget):
ax.scatter(data_x, data_y, alpha=0.6)
if widget.has_selection:
idx = widget.get_indices(data_x, data_y)
ax.scatter(data_x[idx], data_y[idx], color='red')
select = ChartSelect.from_callback(
draw_fn=draw_chart,
x_bounds=(-3, 3),
y_bounds=(-3, 3),
)
Source code in wigglystuff/chart_select.py
get_bounds #
Get bounding box of selection in data coordinates.
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, float] | None
|
(x_min, y_min, x_max, y_max) or None if no selection. |
Source code in wigglystuff/chart_select.py
get_indices #
Return indices of points inside the selection.
Useful for filtering dataframes with df.iloc[indices].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_arr
|
Array-like of x coordinates. |
required | |
y_arr
|
Array-like of y coordinates. |
required |
Returns:
| Type | Description |
|---|---|
|
Numpy array of integer indices for points inside the selection. |
Source code in wigglystuff/chart_select.py
get_mask #
Return boolean mask for points inside the selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_arr
|
Array-like of x coordinates. |
required | |
y_arr
|
Array-like of y coordinates. |
required |
Returns:
| Type | Description |
|---|---|
|
Boolean numpy array where True means point is inside selection. |
Source code in wigglystuff/chart_select.py
get_vertices #
Get selection vertices in data coordinates.
For box mode, returns the 4 corners (clockwise from bottom-left). For lasso mode, returns the path vertices.
Returns:
| Type | Description |
|---|---|
list[tuple[float, float]]
|
List of (x, y) tuples, or empty list if no selection. |
Source code in wigglystuff/chart_select.py
redraw #
Re-render the chart using the stored callback.
Only available for widgets created via from_callback(). Call this
when external state that affects the chart has changed.
Source code in wigglystuff/chart_select.py
Synced traitlets#
| Traitlet | Type | Notes |
|---|---|---|
mode |
str |
Selection mode: "box" or "lasso". |
modes |
list[str] |
Available modes (controls which buttons are shown). |
selection |
dict |
Selection data in data coordinates. Box: {x_min, y_min, x_max, y_max}. Lasso: {vertices: [[x, y], ...]}. |
has_selection |
bool |
Whether a selection is currently active. |
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_color |
str |
CSS color for selection fill and stroke. |
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 |
Clear the current selection. |
get_bounds() |
tuple or None |
Bounding box (x_min, y_min, x_max, y_max) in data coordinates. |
get_vertices() |
list[tuple] |
Selection vertices as (x, y) tuples. |
contains_point(x, y) |
bool |
Check if a point is inside the selection. |
get_mask(x_arr, y_arr) |
ndarray[bool] |
Boolean mask for points inside selection. |
get_indices(x_arr, y_arr) |
ndarray[int] |
Indices of points inside selection. |
redraw() |
None |
Re-render chart (only for from_callback widgets). |