Skip to content
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
27 changes: 20 additions & 7 deletions lib/mpl_toolkits/mplot3d/art3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,18 +819,26 @@ def do_3d_projection(self):
return np.nan

def _maybe_depth_shade_and_sort_colors(self, color_array):
color_array = (
_zalpha(
# Adjust the color_array alpha values if point depths are defined
# and depth shading is active
alpha = self._alpha
if self._vzs is not None and self._depthshade:
color_array = _zalpha(
color_array,
self._vzs,
min_alpha=self._depthshade_minalpha,
)
if self._vzs is not None and self._depthshade
else color_array
)
if alpha is not None and color_array.shape[1] == 4: # RGBA, not RGB
alpha = alpha * color_array[:, 3]

# Adjust the order of the color_array using the _z_markers_idx,
# which has been sorted by z-depth
if len(color_array) > 1:
color_array = color_array[self._z_markers_idx]
return mcolors.to_rgba_array(color_array, self._alpha)
if np.ndim(alpha) > 0:
alpha = np.asarray(alpha)[self._z_markers_idx]

return mcolors.to_rgba_array(color_array, alpha)

def get_facecolor(self):
return self._maybe_depth_shade_and_sort_colors(super().get_facecolor())
Expand Down Expand Up @@ -1070,20 +1078,25 @@ def _use_zordered_offset(self):
def _maybe_depth_shade_and_sort_colors(self, color_array):
# Adjust the color_array alpha values if point depths are defined
# and depth shading is active
alpha = self._alpha
if self._vzs is not None and self._depthshade:
color_array = _zalpha(
color_array,
self._vzs,
min_alpha=self._depthshade_minalpha,
_data_scale=self._data_scale,
)
if alpha is not None and color_array.shape[1] == 4: # RGBA, not RGB
alpha = alpha * color_array[:, 3]

# Adjust the order of the color_array using the _z_markers_idx,
# which has been sorted by z-depth
if len(color_array) > 1:
color_array = color_array[self._z_markers_idx]
if np.ndim(alpha) > 0:
alpha = np.asarray(alpha)[self._z_markers_idx]

return mcolors.to_rgba_array(color_array)
return mcolors.to_rgba_array(color_array, alpha)

def get_facecolor(self):
return self._maybe_depth_shade_and_sort_colors(super().get_facecolor())
Expand Down
13 changes: 13 additions & 0 deletions lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,19 @@ def test_scatter3d_linewidth():
marker='o', linewidth=np.arange(10))


@check_figures_equal()
def test_scatter3d_cmap_alpha(fig_ref, fig_test):
# Check that alpha is applied correctly with colormapped scatter.
# Regression test for https://github.com/matplotlib/matplotlib/issues/25468
x, y, z = np.arange(5), np.zeros(5), np.arange(5)
c = np.array([0, 1, np.nan, 3, 4])

ax_test = fig_test.add_subplot(projection='3d')
ax_test.scatter(x, y, z, c=c)
ax_ref = fig_ref.add_subplot(projection='3d')
ax_ref.scatter(x, y, z, c=c, alpha=1)


@check_figures_equal()
def test_scatter3d_linewidth_modification(fig_ref, fig_test):
# Changing Path3DCollection linewidths with array-like post-creation
Expand Down
Loading