@@ -1073,7 +1073,7 @@ def draw_artist(self, a):
1073
1073
def get_axes (self ):
1074
1074
return self .axes
1075
1075
1076
- def legend (self , handles , labels , * args , ** kwargs ):
1076
+ def legend (self , * args , ** kwargs ):
1077
1077
"""
1078
1078
Place a legend in the figure. Labels are a sequence of
1079
1079
strings, handles is a sequence of
@@ -1083,6 +1083,12 @@ def legend(self, handles, labels, *args, **kwargs):
1083
1083
1084
1084
USAGE::
1085
1085
1086
+ To make a legend from existing artists on every axes::
1087
+
1088
+ legend()
1089
+
1090
+ To make a legend for a list of lines and labels::
1091
+
1086
1092
legend( (line1, line2, line3),
1087
1093
('label1', 'label2', 'label3'),
1088
1094
'upper right')
@@ -1164,6 +1170,25 @@ def legend(self, handles, labels, *args, **kwargs):
1164
1170
1165
1171
.. plot:: mpl_examples/pylab_examples/figlegend_demo.py
1166
1172
"""
1173
+
1174
+ if len (args ) == 0 :
1175
+ ldict = {}
1176
+ for ax in self .axes :
1177
+ handles , labels = ax .get_legend_handles_labels ()
1178
+ ldict = dict (ldict , ** dict (zip (labels , handles )))
1179
+
1180
+ handles , labels = ldict .values (), ldict .keys ()
1181
+ if len (handles ) == 0 :
1182
+ warnings .warn ("No labeled objects found. "
1183
+ "Use label='...' kwarg on individual plots." )
1184
+ return None
1185
+
1186
+ elif len (args ) == 2 :
1187
+ handles , labels = args
1188
+
1189
+ else :
1190
+ raise TypeError ('Invalid arguments to legend' )
1191
+
1167
1192
l = Legend (self , handles , labels , * args , ** kwargs )
1168
1193
self .legends .append (l )
1169
1194
l ._remove_method = lambda h : self .legends .remove (h )
0 commit comments