From: <lee...@us...> - 2009-05-05 17:27:28
|
Revision: 7085 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7085&view=rev Author: leejjoon Date: 2009-05-05 17:27:23 +0000 (Tue, 05 May 2009) Log Message: ----------- Add Axes.get_legend_handles_labels method Modified Paths: -------------- trunk/matplotlib/CHANGELOG trunk/matplotlib/lib/matplotlib/axes.py Modified: trunk/matplotlib/CHANGELOG =================================================================== --- trunk/matplotlib/CHANGELOG 2009-05-05 03:27:48 UTC (rev 7084) +++ trunk/matplotlib/CHANGELOG 2009-05-05 17:27:23 UTC (rev 7085) @@ -1,4 +1,6 @@ ====================================================================== +2009-05-05 Add Axes.get_legend_handles_labels method -JJL + 2009-05-04 Fix bug that Text.Annotation is still drawn while set to not visible.-JJL Modified: trunk/matplotlib/lib/matplotlib/axes.py =================================================================== --- trunk/matplotlib/lib/matplotlib/axes.py 2009-05-05 03:27:48 UTC (rev 7084) +++ trunk/matplotlib/lib/matplotlib/axes.py 2009-05-05 17:27:23 UTC (rev 7085) @@ -3761,6 +3761,41 @@ return lags, c, a, b xcorr.__doc__ = cbook.dedent(xcorr.__doc__) % martist.kwdocd + + def _get_legend_handles(self): + "return artists that will be used as handles for legend" + handles = self.lines[:] + handles.extend(self.patches) + handles.extend([c for c in self.collections + if isinstance(c, mcoll.LineCollection)]) + handles.extend([c for c in self.collections + if isinstance(c, mcoll.RegularPolyCollection)]) + return handles + + + def get_legend_handles_labels(self): + """ + return handles and labels for legend + + ax.legend() is equibalent to :: + + h, l = ax.get_legend_handles_labels() + ax.legend(h, l) + + """ + + handles = [] + labels = [] + for handle in self._get_legend_handles(): + label = handle.get_label() + if (label is not None and + label != '' and not label.startswith('_')): + handles.append(handle) + labels.append(label) + + return handles, labels + + def legend(self, *args, **kwargs): """ call signature:: @@ -3867,24 +3902,8 @@ .. plot:: mpl_examples/api/legend_demo.py """ - def get_handles(): - handles = self.lines[:] - handles.extend(self.patches) - handles.extend([c for c in self.collections - if isinstance(c, mcoll.LineCollection)]) - handles.extend([c for c in self.collections - if isinstance(c, mcoll.RegularPolyCollection)]) - return handles - if len(args)==0: - handles = [] - labels = [] - for handle in get_handles(): - label = handle.get_label() - if (label is not None and - label != '' and not label.startswith('_')): - handles.append(handle) - labels.append(label) + handles, labels = self.get_legend_handles_labels() if len(handles) == 0: warnings.warn("No labeled objects found. " "Use label='...' kwarg on individual plots.") @@ -3893,13 +3912,15 @@ elif len(args)==1: # LABELS labels = args[0] - handles = [h for h, label in zip(get_handles(), labels)] + handles = [h for h, label in zip(self._get_legend_handles(), + labels)] elif len(args)==2: if is_string_like(args[1]) or isinstance(args[1], int): # LABELS, LOC labels, loc = args - handles = [h for h, label in zip(get_handles(), labels)] + handles = [h for h, label in zip(self._get_legend_handles(), + labels)] kwargs['loc'] = loc else: # LINES, LABELS This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |