Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alpha property to scatter artist #27

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
57 changes: 45 additions & 12 deletions docs/examples/scatter_artist_example.ipynb

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/biaplotter/_tests/test_artists.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
assert np.all(colors[0] == scatter.categorical_colormap(0))
assert np.all(colors[50] == scatter.categorical_colormap(2))

# Test alpha property
collected_alpha_signals = []
def on_alpha_changed(alpha):
collected_alpha_signals.append(alpha)

Check warning on line 50 in src/biaplotter/_tests/test_artists.py

View check run for this annotation

Codecov / codecov/patch

src/biaplotter/_tests/test_artists.py#L50

Added line #L50 was not covered by tests
scatter.alpha = np.linspace(start=0.1, stop=1.0, num=size)
assert np.all(scatter.alpha == np.linspace(start=0.1, stop=1.0, num=size))
assert scatter._scatter.get_alpha() is not None


def test_histogram2d():
# Inputs
Expand Down
33 changes: 31 additions & 2 deletions src/biaplotter/artists.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
a colormap to use for the artist, by default cat10_mod_cmap from nap-plot-tools
color_indices : (N,) np.ndarray[int] or int, optional
array of indices to map to the colormap, by default None
alpha : (N,) np.ndarray[float] or float, optional
array of alpha values for the scatter points, by default 1.0

Notes
-----
Expand All @@ -115,19 +117,21 @@
>>> scatter.data = data
>>> scatter.visible = True
>>> scatter.color_indices = np.linspace(start=0, stop=5, num=100, endpoint=False, dtype=int)
>>> scatter.alpha = np.linspace(start=0.1, stop=1.0, num=100)
>>> plt.show()
"""
#: Signal emitted when the `data` is changed.
data_changed_signal: Signal = Signal(np.ndarray)
#: Signal emitted when the `color_indices` are changed.
color_indices_changed_signal: Signal = Signal(np.ndarray)

def __init__(self, ax: plt.Axes = None, data: np.ndarray = None, categorical_colormap: Colormap = cat10_mod_cmap, color_indices: np.ndarray = None):
def __init__(self, ax: plt.Axes = None, data: np.ndarray = None, categorical_colormap: Colormap = cat10_mod_cmap, color_indices: np.ndarray = None, alpha: np.ndarray = 1.0):
"""Initializes the scatter plot artist.
"""
super().__init__(ax, data, categorical_colormap, color_indices)
#: Stores the scatter plot matplotlib object
self._scatter = None
self._alpha = alpha
self.data = data
self.draw() # Initial draw of the scatter plot

Expand Down Expand Up @@ -165,7 +169,8 @@
else:
# If the scatter plot already exists, just update its data
self._scatter.set_offsets(value)

self._scatter.set_alpha(1)

Check warning on line 172 in src/biaplotter/artists.py

View check run for this annotation

Codecov / codecov/patch

src/biaplotter/artists.py#L172

Added line #L172 was not covered by tests

if self._color_indices is None:
self.color_indices = 0 # Set default color index
else:
Expand Down Expand Up @@ -236,6 +241,30 @@
self.color_indices_changed_signal.emit(self._color_indices)
self.draw()

@property
def alpha(self) -> np.ndarray:
"""Gets or sets the alpha values for the scatter plot.

Triggers a draw idle command.

Returns
-------
alpha : (N,) np.ndarray[float] or float
alpha values for the scatter plot. Accepts a scalar or an array of floats.
"""
return self._alpha

@alpha.setter
def alpha(self, value: np.ndarray):
"""Sets the alpha values for the scatter plot and updates the display accordingly."""
# Check if value is a scalar
if np.isscalar(value):
value = np.full(len(self._data), value)

Check warning on line 262 in src/biaplotter/artists.py

View check run for this annotation

Codecov / codecov/patch

src/biaplotter/artists.py#L262

Added line #L262 was not covered by tests
self._alpha = value
if self._scatter is not None:
self._scatter.set_alpha(value)
self.draw()

def draw(self):
"""Draws or redraws the scatter plot."""
self.ax.figure.canvas.draw_idle()
Expand Down
Loading