Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
acab9d0
Allow use of `add_*_selector` methods in `ScatterGraphic`
lkeegan Jul 21, 2025
5ae771e
Update linear/rectangle selectors to work with ScatterGraphics, add e…
lkeegan Jul 22, 2025
c5ec080
refactor bounds_init logic to make intent clearer, add comment, remov…
lkeegan Jul 23, 2025
75b7d73
restore y-padding on rectangle selector limits, refactored to also wo…
lkeegan Jul 23, 2025
c28163f
Merge branch 'main' into scatter_graphic_add_linear_selector
lkeegan Sep 29, 2025
e2d6b9f
Add `add_polygon_selector` method to `ScatterGraphic` and create an e…
lkeegan Sep 30, 2025
8776b29
allow selection to extend by 25% padding in all directions
lkeegan Oct 1, 2025
7898ad2
enable ground truth screenshots for scatter selector examples
lkeegan Oct 1, 2025
82870b9
set initial polygon selection in example and reset _move_info after s…
lkeegan Oct 1, 2025
9054ae0
Merge branch 'main' into scatter_graphic_add_linear_selector
lkeegan Nov 10, 2025
2b607b5
Update fastplotlib/graphics/selectors/_polygon.py
lkeegan Jan 30, 2026
5313a05
Update fastplotlib/graphics/selectors/_rectangle.py
lkeegan Jan 30, 2026
481138b
Update fastplotlib/graphics/selectors/_polygon.py
lkeegan Jan 30, 2026
41c3a02
run black on examples/selection_tools/linear_region_scatter.py
lkeegan Jan 30, 2026
4db8d34
Merge branch 'main' into scatter_graphic_add_linear_selector
lkeegan Jan 30, 2026
6e5b923
update add_*_selector to match main branch (keeping previous changes …
lkeegan Jan 30, 2026
4598ae0
fix typos
lkeegan Jan 30, 2026
b0720b4
Add sizes to linear region scatter example
lkeegan Jan 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions examples/selection_tools/linear_region_scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
LinearRegionSelectors with ScatterGraphic
=========================================

Example showing how to use a `LinearRegionSelector` with a scatter plot. We demonstrate two use cases, a horizontal
LinearRegionSelector which selects along the x-axis and a vertical selector which moves along the y-axis.
"""

# test_example = false

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this true, we'd want to have a ground truth screenshot for this since it's a new feature.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

# sphinx_gallery_pygfx_docs = 'screenshot'

import fastplotlib as fpl
import numpy as np

# names for out subplots
names = [
["scatter x", "scatter y"],
["zoomed x region", "zoomed y region"]
]

# 2 rows, 2 columns
figure = fpl.Figure(
(2, 2),
size=(700, 560),
names=names,
)

scatter_x_data = (100*np.random.random_sample(size=(500, 2))).astype(np.float32)
scatter_y_data = (100*np.random.random_sample(size=(500, 2))).astype(np.float32)
Comment thread
lkeegan marked this conversation as resolved.
Resolved

# plot scatter data
scatter_x = figure[0, 0].add_scatter(scatter_x_data)
scatter_y = figure[0, 1].add_scatter(scatter_y_data)

# add linear selectors
selector_x = scatter_x.add_linear_region_selector((0, 100)) # default axis is "x"
selector_y = scatter_y.add_linear_region_selector(axis="y")

@selector_x.add_event_handler("selection")
def set_zoom_x(ev):
"""sets zoomed x selector data"""
selected_data = ev.get_selected_data()
figure[1, 0].clear()
figure[1, 0].add_scatter(selected_data, sizes=10)
figure[1, 0].auto_scale()


@selector_y.add_event_handler("selection")
def set_zoom_y(ev):
"""sets zoomed y selector data"""
selected_data = ev.get_selected_data()
figure[1, 1].clear()
figure[1, 1].add_scatter(selected_data, sizes=10)
figure[1, 1].auto_scale()

# set initial selection
selector_x.selection = (30, 60)
selector_y.selection = (30, 60)

figure.show(maintain_aspect=False)

# NOTE: fpl.loop.run() should not be used for interactive sessions
# See the "JupyterLab and IPython" section in the user guide
if __name__ == "__main__":
print(__doc__)
fpl.loop.run()
49 changes: 49 additions & 0 deletions examples/selection_tools/rectangle_selector_scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Rectangle Selectors with ScatterGraphic
=======================================

Example showing how to use a `RectangleSelector` with a scatter plot.
"""

# test_example = false

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this true, we'd want to have a ground truth screenshot for this since it's a new feature.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

# sphinx_gallery_pygfx_docs = 'screenshot'

import numpy as np
import fastplotlib as fpl

# create a figure
figure = fpl.Figure(
size=(700, 560)
)

xys = (100 * np.random.random_sample(size=(200, 2))).astype(np.float32)

# add image
scatter = figure[0, 0].add_scatter(xys, cmap="jet", sizes=4)

# add rectangle selector to image graphic
rectangle_selector = scatter.add_rectangle_selector()

# add event handler to highlight selected indices
@rectangle_selector.add_event_handler("selection")
def color_indices(ev):
scatter.cmap = "jet"
scatter.sizes = 4
ixs = ev.get_selected_indices()
if ixs.size == 0:
return
scatter.colors[ixs] = 'w'
scatter.sizes[ixs] = 8


# manually move selector to make a nice gallery image :D
rectangle_selector.selection = (20, 40, 40, 60)


figure.show()

# NOTE: fpl.loop.run() should not be used for interactive sessions
# See the "JupyterLab and IPython" section in the user guide
if __name__ == "__main__":
print(__doc__)
fpl.loop.run()
17 changes: 11 additions & 6 deletions fastplotlib/graphics/_positions_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ def add_rectangle_selector(
"""
# computes args to create selectors
n_datapoints = self.data.value.shape[0]
value_25p = int(n_datapoints / 4)

# remove any nans
data = self.data.value[~np.any(np.isnan(self.data.value), axis=1)]
Expand All @@ -293,13 +292,15 @@ def add_rectangle_selector(

ymin = np.floor(y_axis_vals.min()).astype(int)
ymax = np.ceil(y_axis_vals.max()).astype(int)
xmin = np.floor(x_axis_vals.min()).astype(int)
xmax = np.ceil(x_axis_vals.max()).astype(int)

# default selection is 25% of the image
if selection is None:
selection = (x_axis_vals[0], x_axis_vals[value_25p], ymin, ymax)
selection = (xmin, xmin + 0.25 * (xmax - xmin), ymin, ymax)

# min/max limits
limits = (x_axis_vals[0], x_axis_vals[-1], ymin * 1.5, ymax * 1.5)
limits = (xmin, xmax, ymin, ymax)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the ymin * 1.5 and ymax * 1.5? I think we had that there to allow the selector to go a little bit above and below the graphic.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry - I removed it because with positive ymin the limits didn't allow you to select all the data, restored in 75b7d73


selector = RectangleSelector(
selection=selection,
Expand All @@ -318,7 +319,6 @@ def _get_linear_selector_init_args(
) -> tuple[tuple[float, float], tuple[float, float], float, float]:
# computes args to create selectors
n_datapoints = self.data.value.shape[0]
value_25p = int(n_datapoints / 4)

# remove any nans
data = self.data.value[~np.any(np.isnan(self.data.value), axis=1)]
Expand All @@ -333,8 +333,13 @@ def _get_linear_selector_init_args(
axis_vals = data[:, 1]
magn_vals = data[:, 0]

bounds_init = axis_vals[0], axis_vals[value_25p]
limits = axis_vals[0], axis_vals[-1]
axis_vals_min = np.floor(axis_vals.min()).astype(int)
axis_vals_max = np.floor(axis_vals.max()).astype(int)

bounds_init = axis_vals_min, axis_vals_min + 0.25 * (
axis_vals_max - axis_vals_min
)
limits = axis_vals_min, axis_vals_max
Comment on lines +323 to +329

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add code comments here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure: c5ec080


# width or height of selector
size = int(np.ptp(magn_vals) * 1.5 + padding)
Expand Down
3 changes: 3 additions & 0 deletions fastplotlib/graphics/selectors/_linear_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ def get_selected_data(
# slice with min, max is faster than using all the indices
return source.data[s]

if "Scatter" in source.__class__.__name__:
return source.data[ixs]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this always valid for an arbitrary scatter? What if we have a blob, like a Gaussian cloud? I think it's only valid for scatters that represent data which are a function of the x or y dim?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's valid for any scatter data - it just returns the subset of data in the selected region:

image


if "Image" in source.__class__.__name__:
s = slice(ixs[0], ixs[-1] + 1)

Expand Down
5 changes: 4 additions & 1 deletion fastplotlib/graphics/selectors/_rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,10 @@ def get_selected_indices(
row_ixs = np.arange(ymin, ymax, dtype=int)
return row_ixs, col_ixs

if "Line" in source.__class__.__name__:
if (
"Line" in source.__class__.__name__
or "Scatter" in source.__class__.__name__
):
Comment thread
lkeegan marked this conversation as resolved.
Resolved
if isinstance(source, GraphicCollection):
ixs = list()
for g in source.graphics:
Expand Down