ChartPuck API#
Bases: AnyWidget
Draggable puck overlay for matplotlib charts.
Allows interactive selection of coordinates on a static matplotlib chart. Supports single or multiple pucks. The puck positions are tracked in data coordinates.
Examples:
Single puck:
import matplotlib.pyplot as plt
from wigglystuff import ChartPuck
fig, ax = plt.subplots()
ax.scatter([1, 2, 3], [1, 2, 3])
puck = ChartPuck(fig)
# puck.x, puck.y track the puck position in data coordinates
Multiple pucks:
puck = ChartPuck(fig, x=[0.5, 1.5, 2.5], y=[0.5, 1.5, 2.5])
# puck.x, puck.y are lists of coordinates
Create a ChartPuck widget from a matplotlib figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
A matplotlib figure to overlay the puck on. |
required | |
x
|
float | list[float] | None
|
Initial x coordinate(s) in data space. Can be a single value or a list for multiple pucks. Defaults to center of x_bounds. |
None
|
y
|
float | list[float] | None
|
Initial y coordinate(s) in data space. Can be a single value or a list for multiple pucks. Defaults to center of y_bounds. |
None
|
puck_radius
|
int
|
Radius of the puck(s) in pixels. |
10
|
puck_color
|
str
|
Color of the puck(s) (any CSS color). |
'#e63946'
|
drag_x_bounds
|
tuple[float, float] | None
|
Optional (min, max) to constrain puck dragging on x-axis. If None, uses the chart's x_bounds. |
None
|
drag_y_bounds
|
tuple[float, float] | None
|
Optional (min, max) to constrain puck dragging on y-axis. If None, uses the chart's y_bounds. |
None
|
**kwargs
|
Any
|
Forwarded to |
{}
|
Source code in wigglystuff/chart_puck.py
export_kmeans #
Export puck positions as a KMeans estimator with pucks as initial centroids.
Creates a scikit-learn KMeans instance configured to use the current puck positions as initial cluster centers. Useful for interactive clustering where you manually position pucks to define cluster centers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_init
|
int
|
Number of initializations. Defaults to 1 since we're providing explicit initial centers. |
1
|
max_iter
|
int
|
Maximum iterations for the algorithm. |
300
|
**kwargs
|
Additional arguments passed to sklearn.cluster.KMeans. |
{}
|
Returns:
| Type | Description |
|---|---|
|
A sklearn.cluster.KMeans instance ready to fit on data. |
Examples:
puck = ChartPuck(fig, x=[1.0, 3.0], y=[1.0, 3.0])
# ... user drags pucks to desired positions ...
kmeans = puck.export_kmeans()
labels = kmeans.fit_predict(data)
Source code in wigglystuff/chart_puck.py
from_callback
classmethod
#
from_callback(
draw_fn,
x_bounds: tuple[float, float],
y_bounds: tuple[float, float],
figsize: tuple[float, float] = (6, 6),
x: float | list[float] | None = None,
y: float | list[float] | None = None,
drag_x_bounds: tuple[float, float] | None = None,
drag_y_bounds: tuple[float, float] | None = None,
**kwargs: Any
) -> "ChartPuck"
Create a ChartPuck that auto-updates when the puck moves.
This is the recommended way to create a ChartPuck when you want the chart
to update dynamically as the puck is dragged. The callback function is
called on init and whenever the puck position changes. Use redraw()
to manually trigger a re-render (e.g., when external state changes).
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.x and widget.y (lists of all puck positions). Called on init and whenever puck position changes. 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)
|
x
|
float | list[float] | None
|
Initial x position(s). Defaults to center of x_bounds. |
None
|
y
|
float | list[float] | None
|
Initial y position(s). Defaults to center of y_bounds. |
None
|
drag_x_bounds
|
tuple[float, float] | None
|
Optional (min, max) to constrain puck dragging on x-axis. |
None
|
drag_y_bounds
|
tuple[float, float] | None
|
Optional (min, max) to constrain puck dragging on y-axis. |
None
|
**kwargs
|
Any
|
Passed to ChartPuck (puck_radius, puck_color, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
'ChartPuck'
|
A ChartPuck instance with auto-update behavior and redraw() method. |
Examples:
def draw_chart(ax, widget):
ax.scatter(data_x, data_y, alpha=0.6)
ax.axvline(widget.x[0], color='red', linestyle='--')
ax.axhline(widget.y[0], color='red', linestyle='--')
puck = ChartPuck.from_callback(
draw_fn=draw_chart,
x_bounds=(-3, 3),
y_bounds=(-3, 3),
figsize=(6, 6),
x=0, y=0
)
# Manually trigger redraw (e.g., when external state changes)
puck.redraw()
Source code in wigglystuff/chart_puck.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
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 (e.g., a dropdown
selection) to trigger a re-render without moving the pucks.
Source code in wigglystuff/chart_puck.py
Synced traitlets#
| Traitlet | Type | Notes |
|---|---|---|
x |
list[float] |
Puck x-coordinates in data space. |
y |
list[float] |
Puck y-coordinates in data space. |
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. |
puck_radius |
int |
Radius of puck(s) in pixels. |
puck_color |
str |
CSS color of puck(s). |