Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d546493

Browse files
committed
ENH: Separate facecolor alpha and edgecolor alpha for patches
1 parent cca039d commit d546493

2 files changed

Lines changed: 57 additions & 10 deletions

File tree

lib/matplotlib/patches.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def set_antialiased(self, aa):
314314
self._antialiased = aa
315315
self.stale = True
316316

317-
def _set_edgecolor(self, color):
317+
def _set_edgecolor(self, color, edge_alpha=0):
318318
set_hatch_color = True
319319
if color is None:
320320
if (mpl.rcParams['patch.force_edgecolor'] or
@@ -323,8 +323,10 @@ def _set_edgecolor(self, color):
323323
else:
324324
color = 'none'
325325
set_hatch_color = False
326-
327-
self._edgecolor = colors.to_rgba(color, self._alpha)
326+
if edge_alpha:
327+
self._edgecolor = colors.to_rgba(color, edge_alpha)
328+
else:
329+
self._edgecolor = colors.to_rgba(color, self._alpha)
328330
if set_hatch_color:
329331
self._hatch_color = self._edgecolor
330332
self.stale = True
@@ -340,10 +342,13 @@ def set_edgecolor(self, color):
340342
self._original_edgecolor = color
341343
self._set_edgecolor(color)
342344

343-
def _set_facecolor(self, color):
345+
def _set_facecolor(self, color, face_alpha=0):
344346
if color is None:
345347
color = mpl.rcParams['patch.facecolor']
346-
alpha = self._alpha if self._fill else 0
348+
if face_alpha and self._fill:
349+
alpha = face_alpha
350+
else:
351+
alpha = self._alpha if self._fill else 0
347352
self._facecolor = colors.to_rgba(color, alpha)
348353
self.stale = True
349354

@@ -375,11 +380,37 @@ def set_color(self, c):
375380
self.set_edgecolor(c)
376381

377382
def set_alpha(self, alpha):
378-
# docstring inherited
379-
super().set_alpha(alpha)
380-
self._set_facecolor(self._original_facecolor)
381-
self._set_edgecolor(self._original_edgecolor)
382-
# stale is already True
383+
"""
384+
Set the alpha value used for blending.
385+
386+
Parameters
387+
----------
388+
alpha : scalar or None or tuple of scalars
389+
*alpha* must be within the 0-1 range, inclusive.
390+
"""
391+
is_alpha_valid_tuple = False
392+
if isinstance(alpha, tuple):
393+
if len(alpha) != 2:
394+
raise ValueError('If given as a tuple, alpha must contain two '
395+
'numbers, the face and edge alpha values')
396+
for item in alpha:
397+
if not isinstance(item, Number):
398+
raise TypeError('Alpha values must be numbers between 0 '
399+
'and 1, inclusive')
400+
if not (0 <= item <= 1):
401+
raise ValueError('Alpha values must be between 0 and 1, '
402+
'inclusive')
403+
is_alpha_valid_tuple = True
404+
if is_alpha_valid_tuple:
405+
face_alpha = alpha[0]
406+
edge_alpha = alpha[1]
407+
self._set_facecolor(self._original_facecolor, face_alpha)
408+
self._set_edgecolor(self._original_edgecolor, edge_alpha)
409+
else:
410+
super().set_alpha(alpha)
411+
self._set_facecolor(self._original_facecolor)
412+
self._set_edgecolor(self._original_edgecolor)
413+
# stale is already True
383414

384415
def set_linewidth(self, w):
385416
"""

lib/matplotlib/tests/test_patches.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,3 +880,19 @@ def test_modifying_arc(fig_test, fig_ref):
880880
fig_test.subplots().add_patch(arc2)
881881
arc2.set_width(.5)
882882
arc2.set_angle(20)
883+
884+
885+
@check_figures_equal()
886+
def test_alpha_as_tuple(fig_test, fig_ref):
887+
patch_test = Rectangle((0, 0), 1, 1,
888+
edgecolor='black',
889+
facecolor='red',
890+
alpha=(0.1, 1))
891+
ax = fig_test.add_subplot(111)
892+
ax.add_patch(patch_test)
893+
894+
patch_ref = Rectangle((0, 0), 1, 1,
895+
edgecolor='black',
896+
facecolor=(*mcolors.to_rgb('red'), 0.1))
897+
ax = fig_ref.add_subplot(111)
898+
ax.add_patch(patch_ref)

0 commit comments

Comments
 (0)