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

Skip to content

Commit 09191a4

Browse files
committed
BUG : correct default value alpha on legend patch
Fixes #3766 Adding `legend.framealpha` in #3369 changed the default value of alpha on the legend patch from `None` -> 1 which has no visual effect as `to_rgba` defaults to alpha=1 if no alpha is passed, but this overrides setting the alpha component via `set_facecolor(rgba_value)`. This PR - addes a new validation function `validate_float_or_None` - changes legend.framealpha to default to `None` - adds test that with the default value of `legend.framealpha` the alpha value of the face can be set via a rgba value - adds tests that setting the framealpha to a non-None value ignores the alpha value passed to set_facecolor
1 parent 001b1e9 commit 09191a4

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ def validate_float(s):
108108
raise ValueError('Could not convert "%s" to float' % s)
109109

110110

111+
def validate_float_or_None(s):
112+
"""convert s to float or raise"""
113+
if s is None:
114+
return None
115+
try:
116+
return float(s)
117+
except ValueError:
118+
raise ValueError('Could not convert "%s" to float' % s)
119+
120+
111121
def validate_int(s):
112122
"""convert s to int or raise"""
113123
try:
@@ -640,7 +650,7 @@ def __call__(self, s):
640650
# whether or not to draw a frame around legend
641651
'legend.frameon': [True, validate_bool],
642652
# alpha value of the legend frame
643-
'legend.framealpha': [1.0, validate_float],
653+
'legend.framealpha': [None, validate_float_or_None],
644654

645655
## the following dimensions are in fraction of the font size
646656
'legend.borderpad': [0.4, validate_float], # units are fontsize

lib/matplotlib/tests/test_legend.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ def test_various_labels():
6464
ax.legend(numpoints=1, loc=0)
6565

6666

67+
@image_comparison(baseline_images=['rgba_alpha'],
68+
extensions=['png'], remove_text=True)
69+
def test_alpha_rgba():
70+
import matplotlib.pyplot as plt
71+
72+
fig, ax = plt.subplots(1, 1)
73+
ax.plot(range(10), lw=5)
74+
leg = plt.legend(['Longlabel that will go away'], loc=10)
75+
leg.legendPatch.set_facecolor([1, 0, 0, 0.5])
76+
77+
78+
@image_comparison(baseline_images=['rcparam_alpha'],
79+
extensions=['png'], remove_text=True)
80+
def test_alpha_rcparam():
81+
import matplotlib.pyplot as plt
82+
83+
fig, ax = plt.subplots(1, 1)
84+
ax.plot(range(10), lw=5)
85+
with mpl.rc_context(rc={'legend.framealpha': .75}):
86+
leg = plt.legend(['Longlabel that will go away'], loc=10)
87+
# this alpha is going to be over-ridden by the rcparam whith
88+
# sets the alpha of the patch to be non-None which causes the alpha
89+
# value of the face color to be discarded. This behavior may not be
90+
# ideal, but it is what it is and we should keep track of it changing
91+
leg.legendPatch.set_facecolor([1, 0, 0, 0.5])
92+
93+
6794
@image_comparison(baseline_images=['fancy'], remove_text=True)
6895
def test_fancy():
6996
# using subplot triggers some offsetbox functionality untested elsewhere

0 commit comments

Comments
 (0)