diff --git a/examples/pylab_examples/legend_auto.py b/examples/pylab_examples/legend_auto.py index a29a746d13ae..bcab13df3375 100644 --- a/examples/pylab_examples/legend_auto.py +++ b/examples/pylab_examples/legend_auto.py @@ -18,7 +18,7 @@ def fig_1(): figure(1) t = arange(0, 40.0 * pi, 0.1) l, = plot(t, 100*sin(t), 'r', label='sine') - legend() + legend(framealpha=0.5) def fig_2(): figure(2) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index 11b50caf79b9..9be4a2e2be2c 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -4448,6 +4448,9 @@ def legend(self, *args, **kwargs): If *True*, draw a shadow behind legend. If *None*, use rc settings. + *framealpha*: [*None* | float] + If not None, alpha channel for legend frame. Default *None*. + *ncol* : integer number of columns. default is 1 diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index e2fa2957e1b2..07b7f5cd6f1c 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -167,6 +167,9 @@ def __init__(self, parent, handles, labels, # box, none use rc shadow=None, title=None, # set a title for the legend + + framealpha=None, # set frame alpha + bbox_to_anchor=None, # bbox that the legend will be anchored. bbox_transform=None, # transform for the bbox frameon=None, # draw frame @@ -195,6 +198,7 @@ def __init__(self, parent, handles, labels, fancybox if True, draw a frame with a round fancybox. If None, use rc shadow if True, draw a shadow behind legend + framealpha If not None, alpha channel for the frame. ncol number of columns borderpad the fractional whitespace inside the legend border labelspacing the vertical space between the legend entries @@ -384,6 +388,9 @@ def __init__(self, parent, handles, labels, # init with null renderer self._init_legend_box(handles, labels) + if framealpha is not None: + self.get_frame().set_alpha(framealpha) + self._loc = loc self.set_title(title) diff --git a/lib/matplotlib/tests/baseline_images/test_legend/framealpha.pdf b/lib/matplotlib/tests/baseline_images/test_legend/framealpha.pdf new file mode 100644 index 000000000000..397fee82c1ce Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/framealpha.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/framealpha.png b/lib/matplotlib/tests/baseline_images/test_legend/framealpha.png new file mode 100644 index 000000000000..1fbe5c674aff Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/framealpha.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/framealpha.svg b/lib/matplotlib/tests/baseline_images/test_legend/framealpha.svg new file mode 100644 index 000000000000..4a2bc519b45e --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_legend/framealpha.svg @@ -0,0 +1,495 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 06735c5c6928..d9448791d029 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -47,6 +47,12 @@ def test_fancy(): plt.legend(loc="center left", bbox_to_anchor=[1.0, 0.5], ncol=2, shadow=True, title="My legend", numpoints=1) +@image_comparison(baseline_images=['framealpha'], remove_text=True) +def test_framealpha(): + x = np.linspace(1, 100, 100) + y = x + plt.plot(x, y, label='mylabel', lw=10) + plt.legend(framealpha=0.5) @image_comparison(baseline_images=['scatter_rc3','scatter_rc1'], remove_text=True) def test_rc():