Skip to content

Commit

Permalink
Merge branch 'jo-mueller/add-alpha' into jo-mueller/add-size
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-mueller committed Jul 23, 2024
2 parents 064248d + 5b5f420 commit 390e9a9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
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 @@ -62,6 +62,14 @@ def on_color_indices_changed(color_indices):
sizes = scatter._scatter.get_sizes()
assert np.all(sizes == 1.0)

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

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

View check run for this annotation

Codecov / codecov/patch

src/biaplotter/_tests/test_artists.py#L68

Added line #L68 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 @@ class Scatter(Artist):
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,20 +117,22 @@ class Scatter(Artist):
>>> 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._size = 1 # Default size
self._alpha = alpha
self.data = data
self.draw() # Initial draw of the scatter plot

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

self._scatter.set_alpha(1)

if self._color_indices is None:
self.color_indices = 0 # Set default color index
else:
Expand Down Expand Up @@ -238,6 +243,30 @@ def color_indices(self, indices: np.ndarray):
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 264 in src/biaplotter/artists.py

View check run for this annotation

Codecov / codecov/patch

src/biaplotter/artists.py#L264

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

@property
def size(self) -> float | np.ndarray:
"""Gets or sets the size of the points in the scatter plot.
Expand Down

0 comments on commit 390e9a9

Please sign in to comment.