diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index d2650ee33bf4..65410fe1b439 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -108,6 +108,16 @@ def validate_float(s): raise ValueError('Could not convert "%s" to float' % s) +def validate_float_or_None(s): + """convert s to float or raise""" + if s is None: + return None + try: + return float(s) + except ValueError: + raise ValueError('Could not convert "%s" to float' % s) + + def validate_int(s): """convert s to int or raise""" try: @@ -640,7 +650,7 @@ def __call__(self, s): # whether or not to draw a frame around legend 'legend.frameon': [True, validate_bool], # alpha value of the legend frame - 'legend.framealpha': [1.0, validate_float], + 'legend.framealpha': [None, validate_float_or_None], ## the following dimensions are in fraction of the font size 'legend.borderpad': [0.4, validate_float], # units are fontsize diff --git a/lib/matplotlib/tests/baseline_images/test_legend/rcparam_alpha.png b/lib/matplotlib/tests/baseline_images/test_legend/rcparam_alpha.png new file mode 100644 index 000000000000..4296ab2b8760 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/rcparam_alpha.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/rgba_alpha.png b/lib/matplotlib/tests/baseline_images/test_legend/rgba_alpha.png new file mode 100644 index 000000000000..a0912601a2f4 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/rgba_alpha.png differ diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 973a3d9cf672..b655f5655b45 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -64,6 +64,33 @@ def test_various_labels(): ax.legend(numpoints=1, loc=0) +@image_comparison(baseline_images=['rgba_alpha'], + extensions=['png'], remove_text=True) +def test_alpha_rgba(): + import matplotlib.pyplot as plt + + fig, ax = plt.subplots(1, 1) + ax.plot(range(10), lw=5) + leg = plt.legend(['Longlabel that will go away'], loc=10) + leg.legendPatch.set_facecolor([1, 0, 0, 0.5]) + + +@image_comparison(baseline_images=['rcparam_alpha'], + extensions=['png'], remove_text=True) +def test_alpha_rcparam(): + import matplotlib.pyplot as plt + + fig, ax = plt.subplots(1, 1) + ax.plot(range(10), lw=5) + with mpl.rc_context(rc={'legend.framealpha': .75}): + leg = plt.legend(['Longlabel that will go away'], loc=10) + # this alpha is going to be over-ridden by the rcparam whith + # sets the alpha of the patch to be non-None which causes the alpha + # value of the face color to be discarded. This behavior may not be + # ideal, but it is what it is and we should keep track of it changing + leg.legendPatch.set_facecolor([1, 0, 0, 0.5]) + + @image_comparison(baseline_images=['fancy'], remove_text=True) def test_fancy(): # using subplot triggers some offsetbox functionality untested elsewhere diff --git a/matplotlibrc.template b/matplotlibrc.template index 7e0edc723ad9..f742fe36e6d2 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -255,10 +255,10 @@ backend : %(backend)s # separator in the fr_FR locale. #axes.formatter.use_mathtext : False # When True, use mathtext for scientific # notation. -#axes.formatter.useoffset : True # If True, the tick label formatter - # will default to labeling ticks relative - # to an offset when the data range is very - # small compared to the minimum absolute +#axes.formatter.useoffset : True # If True, the tick label formatter + # will default to labeling ticks relative + # to an offset when the data range is very + # small compared to the minimum absolute # value of the data. #axes.unicode_minus : True # use unicode for the minus symbol @@ -320,7 +320,7 @@ backend : %(backend)s #legend.columnspacing : 2. # the border between the axes and legend edge in fraction of fontsize #legend.shadow : False #legend.frameon : True # whether or not to draw a frame around legend -#legend.framealpha : 1.0 # opacity of of legend frame +#legend.framealpha : None # opacity of of legend frame #legend.scatterpoints : 3 # number of scatter points ### FIGURE @@ -399,7 +399,7 @@ backend : %(backend)s #savefig.jpeg_quality: 95 # when a jpeg is saved, the default quality parameter. #savefig.directory : ~ # default directory in savefig dialog box, # leave empty to always use current working directory -#savefig.transparent : False # setting that controls whether figures are saved with a +#savefig.transparent : False # setting that controls whether figures are saved with a # transparent background by default # tk backend params @@ -489,5 +489,5 @@ backend : %(backend)s # $PATH is searched #animation.mencoder_args: '' # Additional arguments to pass to mencoder #animation.convert_path: 'convert' # Path to ImageMagick's convert binary. - # On Windows use the full path since convert + # On Windows use the full path since convert # is also the name of a system tool.