Skip to content

figure.legend can be called without arguments #2128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 78 additions & 8 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,15 +1073,31 @@ def draw_artist(self, a):
def get_axes(self):
return self.axes

def legend(self, handles, labels, *args, **kwargs):
def legend(self, *args, **kwargs):
"""
Place a legend in the figure. Labels are a sequence of
strings, handles is a sequence of
:class:`~matplotlib.lines.Line2D` or
:class:`~matplotlib.patches.Patch` instances, and loc can be a
string or an integer specifying the legend location
Place a legend in the figure.

*labels*
a sequence of strings

USAGE::
*handles*
a sequence of :class:`~matplotlib.lines.Line2D` or
:class:`~matplotlib.patches.Patch` instances

*loc*
can be a string or an integer specifying the legend
location

A :class:`matplotlib.legend.Legend` instance is returned.


Example::

To make a legend from existing artists on every axes::

legend()

To make a legend for a list of lines and labels::

legend( (line1, line2, line3),
('label1', 'label2', 'label3'),
Expand Down Expand Up @@ -1164,7 +1180,61 @@ def legend(self, handles, labels, *args, **kwargs):

.. plot:: mpl_examples/pylab_examples/figlegend_demo.py
"""
l = Legend(self, handles, labels, *args, **kwargs)

if len(args) == 0:
handles = []
labels = []

def in_handles(h, l):
for f_h, f_l in zip(handles, labels):
if f_l != l:
continue
if type(f_h) != type(h):
continue

try:
if f_h.get_color() != h.get_color():
continue
except AttributeError:
pass
try:
if f_h.get_facecolor() != h.get_facecolor():
continue
except AttributeError:
pass
try:
if f_h.get_edgecolor() != h.get_edgecolor():
continue
except AttributeError:
pass

return True
return False

for ax in self.axes:
ax_handles, ax_labels = ax.get_legend_handles_labels()
for h, l in zip(ax_handles, ax_labels):
if not in_handles(h, l):
handles.append(h)
labels.append(l)
if len(handles) == 0:
warnings.warn("No labeled objects found. "
"Use label='...' kwarg on individual plots.")
return None

elif len(args) == 2:
# LINES, LABELS
handles, labels = args

elif len(args) == 3:
# LINES, LABELS, LOC
handles, labels, loc = args
kwargs['loc'] = loc

else:
raise TypeError('Invalid arguments to legend')

l = Legend(self, handles, labels, **kwargs)
self.legends.append(l)
l._remove_method = lambda h: self.legends.remove(h)
return l
Expand Down
10 changes: 8 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def figimage(*args, **kwargs):
return ret


def figlegend(handles, labels, loc, **kwargs):
def figlegend(*args, **kwargs):
"""
Place a legend in the figure.

Expand All @@ -646,6 +646,12 @@ def figlegend(handles, labels, loc, **kwargs):

Example::

To make a legend from existing artists on every axes::

figlegend()

To make a legend for a list of lines and labels::

figlegend( (line1, line2, line3),
('label1', 'label2', 'label3'),
'upper right' )
Expand All @@ -655,7 +661,7 @@ def figlegend(handles, labels, loc, **kwargs):
:func:`~matplotlib.pyplot.legend`

"""
l = gcf().legend(handles, labels, loc, **kwargs)
l = gcf().legend(*args, **kwargs)
draw_if_interactive()
return l

Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading