Canvas Widget#
import napari
import numpy as np
from biaplotter.plotter import CanvasWidget, ArtistType
from napari.utils import nbscreenshot
Adding Canvas Widget to napari#
viewer = napari.Viewer()
canvas_widget = CanvasWidget(viewer)
viewer.window.add_dock_widget(canvas_widget)
nbscreenshot(viewer)
WARNING: QWindowsWindow::setGeometry: Unable to set geometry 2172x1310+728+260 (frame: 2198x1381+715+202) on QWidgetWindow/"_QtMainWindowClassWindow" on "\\.\DISPLAY1". Resulting geometry: 4350x2119+733+287 (frame: 4376x2190+720+229) margins: 13, 58, 13, 13 minimum size: 385x500 MINMAXINFO maxSize=0,0 maxpos=0,0 mintrack=796,1071 maxtrack=0,0)
Check canvas_widget default artists and selectors
canvas_widget.artists
{<ArtistType.SCATTER: 2>: <biaplotter.artists.Scatter at 0x2bc78b5eac0>,
<ArtistType.HISTOGRAM2D: 1>: <biaplotter.artists.Histogram2D at 0x2bc78b5eaf0>}
canvas_widget.selectors
{<SelectorType.LASSO: 1>: <biaplotter.selectors.InteractiveLassoSelector at 0x2bc78b5eb80>,
<SelectorType.ELLIPSE: 2>: <biaplotter.selectors.InteractiveEllipseSelector at 0x2bc78b5eca0>,
<SelectorType.RECTANGLE: 3>: <biaplotter.selectors.InteractiveRectangleSelector at 0x2bc78b5ecd0>}
Generate some data#
def generate_gaussian_data(n_samples):
"""Generate a 2D dataset with two Gaussian clusters."""
# Gaussian 1
x1 = np.random.normal(loc=2, scale=1, size=n_samples//2)
y1 = np.random.normal(loc=2, scale=1, size=n_samples//2)
# Gaussian 2
x2 = np.random.normal(loc=-2, scale=0.5, size=n_samples//2)
y2 = np.random.normal(loc=-2, scale=0.5, size=n_samples//2)
x_data = np.concatenate([x1, x2])
y_data = np.concatenate([y1, y2])
return np.vstack([x_data, y_data]).T
n_samples = 1000
data = generate_gaussian_data(n_samples)
Histogram2D artist is the default active artist.
canvas_widget.active_artist
<biaplotter.artists.Histogram2D at 0x2bc78b5eaf0>
Adding data to the canvas#
Adding data to artist will update the histogram.
canvas_widget.active_artist.data = data
nbscreenshot(viewer)
Similarly, changing the active artist to SCATTER
and adding data to it will update the scatter plot.
canvas_widget.active_artist = canvas_widget.artists[ArtistType.SCATTER]
canvas_widget.active_artist.data = data
nbscreenshot(viewer)
To go back to the histogram, change the active artist to HISTOGRAM2D
.
canvas_widget.active_artist = canvas_widget.artists[ArtistType.HISTOGRAM2D]
Manually selecting data#
Lasso Selector#
Selecting data using lasso selector will update the active artist plot.
Ellipse Selector#
Ellipse selector produces an interactive ellipse that can be dragged and expanded before being applied.
Right-clicking applies the current class to the selected data.
Rectangle Selector#
Rectangle selector produces an interactive rectangle that can be dragged and expanded as well.
One way to clear previuos selections is to apply class 0
to the selected data.