Skip to content

Commit 8aec3fb

Browse files
committed
Merge pull request #2798 from cimarronm/legend_remove
Added remove methods for legends in figure and axes objects
2 parents a4d4031 + d1ad5d2 commit 8aec3fb

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ def legend(self, *args, **kwargs):
509509
raise TypeError('Invalid arguments to legend.')
510510

511511
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
512+
self.legend_._remove_method = lambda h: setattr(self, 'legend_', None)
512513
return self.legend_
513514

514515
def text(self, x, y, s, fontdict=None,

lib/matplotlib/figure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,7 @@ def legend(self, handles, labels, *args, **kwargs):
11661166
"""
11671167
l = Legend(self, handles, labels, *args, **kwargs)
11681168
self.legends.append(l)
1169+
l._remove_method = lambda h: self.legends.remove(h)
11691170
return l
11701171

11711172
@docstring.dedent_interpd

lib/matplotlib/tests/test_legend.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,21 @@ def test_framealpha():
8080
plt.legend(framealpha=0.5)
8181

8282

83-
@image_comparison(baseline_images=['scatter_rc3','scatter_rc1'], remove_text=True)
83+
@image_comparison(baseline_images=['scatter_rc3', 'scatter_rc1'], remove_text=True)
8484
def test_rc():
8585
# using subplot triggers some offsetbox functionality untested elsewhere
8686
fig = plt.figure()
87-
ax = plt.subplot(121)
87+
ax = plt.subplot(121)
8888
ax.scatter(list(xrange(10)), list(xrange(10, 0, -1)), label='three')
8989
ax.legend(loc="center left", bbox_to_anchor=[1.0, 0.5],
90-
title="My legend")
90+
title="My legend")
9191

9292
mpl.rcParams['legend.scatterpoints'] = 1
9393
fig = plt.figure()
94-
ax = plt.subplot(121)
94+
ax = plt.subplot(121)
9595
ax.scatter(list(xrange(10)), list(xrange(10, 0, -1)), label='one')
9696
ax.legend(loc="center left", bbox_to_anchor=[1.0, 0.5],
97-
title="My legend")
97+
title="My legend")
9898

9999

100100
@image_comparison(baseline_images=['legend_expand'], remove_text=True)
@@ -113,6 +113,19 @@ def test_legend_expand():
113113
ax.legend(loc=3, mode=mode, ncol=2)
114114

115115

116+
@cleanup
117+
def test_legend_remove():
118+
fig = plt.figure()
119+
ax = fig.add_subplot(1, 1, 1)
120+
lines = ax.plot(range(10))
121+
leg = fig.legend(lines, "test")
122+
leg.remove()
123+
assert_equal(fig.legends, [])
124+
leg = ax.legend("test")
125+
leg.remove()
126+
assert ax.get_legend() is None
127+
128+
116129
class TestLegendFunction(object):
117130
# Tests the legend function on the Axes and pyplot.
118131

@@ -154,8 +167,8 @@ def __call__(self, legend, orig_handle, fontsize, handlebox):
154167
handler_map={None: AnyObjectHandler()})
155168

156169
warn.assert_called_with(u'Legend handers must now implement a '
157-
'"legend_artist" method rather than '
158-
'being a callable.',
170+
'"legend_artist" method rather than '
171+
'being a callable.',
159172
MatplotlibDeprecationWarning,
160173
stacklevel=1)
161174

0 commit comments

Comments
 (0)