Skip to content

Commit 56e15b5

Browse files
ricmperesmeeseeksmachine
authored andcommitted
Backport PR matplotlib#25478: [BUG] Fix alpha bug on 3D PathCollection plots.
1 parent 9adecab commit 56e15b5

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -819,18 +819,26 @@ def do_3d_projection(self):
819819
return np.nan
820820

821821
def _maybe_depth_shade_and_sort_colors(self, color_array):
822-
color_array = (
823-
_zalpha(
822+
# Adjust the color_array alpha values if point depths are defined
823+
# and depth shading is active
824+
alpha = self._alpha
825+
if self._vzs is not None and self._depthshade:
826+
color_array = _zalpha(
824827
color_array,
825828
self._vzs,
826829
min_alpha=self._depthshade_minalpha,
827830
)
828-
if self._vzs is not None and self._depthshade
829-
else color_array
830-
)
831+
if alpha is not None and color_array.shape[1] == 4: # RGBA, not RGB
832+
alpha = alpha * color_array[:, 3]
833+
834+
# Adjust the order of the color_array using the _z_markers_idx,
835+
# which has been sorted by z-depth
831836
if len(color_array) > 1:
832837
color_array = color_array[self._z_markers_idx]
833-
return mcolors.to_rgba_array(color_array, self._alpha)
838+
if np.ndim(alpha) > 0:
839+
alpha = np.asarray(alpha)[self._z_markers_idx]
840+
841+
return mcolors.to_rgba_array(color_array, alpha)
834842

835843
def get_facecolor(self):
836844
return self._maybe_depth_shade_and_sort_colors(super().get_facecolor())
@@ -1070,20 +1078,25 @@ def _use_zordered_offset(self):
10701078
def _maybe_depth_shade_and_sort_colors(self, color_array):
10711079
# Adjust the color_array alpha values if point depths are defined
10721080
# and depth shading is active
1081+
alpha = self._alpha
10731082
if self._vzs is not None and self._depthshade:
10741083
color_array = _zalpha(
10751084
color_array,
10761085
self._vzs,
10771086
min_alpha=self._depthshade_minalpha,
10781087
_data_scale=self._data_scale,
10791088
)
1089+
if alpha is not None and color_array.shape[1] == 4: # RGBA, not RGB
1090+
alpha = alpha * color_array[:, 3]
10801091

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

1086-
return mcolors.to_rgba_array(color_array)
1099+
return mcolors.to_rgba_array(color_array, alpha)
10871100

10881101
def get_facecolor(self):
10891102
return self._maybe_depth_shade_and_sort_colors(super().get_facecolor())

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,19 @@ def test_scatter3d_linewidth():
440440
marker='o', linewidth=np.arange(10))
441441

442442

443+
@check_figures_equal()
444+
def test_scatter3d_cmap_alpha(fig_ref, fig_test):
445+
# Check that alpha is applied correctly with colormapped scatter.
446+
# Regression test for https://github.com/matplotlib/matplotlib/issues/25468
447+
x, y, z = np.arange(5), np.zeros(5), np.arange(5)
448+
c = np.array([0, 1, np.nan, 3, 4])
449+
450+
ax_test = fig_test.add_subplot(projection='3d')
451+
ax_test.scatter(x, y, z, c=c)
452+
ax_ref = fig_ref.add_subplot(projection='3d')
453+
ax_ref.scatter(x, y, z, c=c, alpha=1)
454+
455+
443456
@check_figures_equal()
444457
def test_scatter3d_linewidth_modification(fig_ref, fig_test):
445458
# Changing Path3DCollection linewidths with array-like post-creation

0 commit comments

Comments
 (0)