Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cdb9681

Browse files
committed
Legend title support
svn path=/trunk/matplotlib/; revision=6914
1 parent 53d7735 commit cdb9681

4 files changed

Lines changed: 57 additions & 10 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2009-02-14 Added the legend title support - JJL
2+
13
2009-02-10 Fixed a bug in backend_pdf so it doesn't break when the setting
24
pdf.use14corefonts=True is used. Added test case in
35
unit/test_pdf_use14corefonts.py. - NGR

examples/pylab_examples/legend_demo3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def myplot(ax):
1818

1919
ax2 = plt.subplot(3,1,2)
2020
myplot(ax2)
21-
ax2.legend(loc=1, ncol=2, shadow=True)
22-
21+
ax2.legend(loc=1, ncol=2, shadow=True, title="Legend")
22+
ax2.get_legend().get_title().set_color("red")
2323

2424
ax3 = plt.subplot(3,1,3)
2525
myplot(ax3)

lib/matplotlib/legend.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ def __init__(self, parent, handles, labels,
110110
mode=None, # mode for horizontal distribution of columns. None, "expand"
111111

112112
fancybox=None, # True use a fancy box, false use a rounded box, none use rc
113-
shadow = None,
113+
shadow = None,
114+
title = None, # set a title for the legend
114115
):
115116
"""
116117
- *parent* : the artist that contains the legend
@@ -135,6 +136,7 @@ def __init__(self, parent, handles, labels,
135136
handletextpad the pad between the legend handle and text
136137
borderaxespad the pad between the axes and legend border
137138
columnspacing the spacing between columns
139+
title the legend title
138140
================ ==================================================================
139141
140142
The dimensions of pad and spacing are given as a fraction of the
@@ -276,6 +278,8 @@ def __init__(self, parent, handles, labels,
276278
# init with null renderer
277279
self._init_legend_box(handles, labels)
278280

281+
self.set_title(title)
282+
279283
self._last_fontsize_points = self._fontsize
280284

281285

@@ -316,6 +320,7 @@ def draw(self, renderer):
316320

317321
renderer.open_group('legend')
318322

323+
319324
# find_offset function will be provided to _legend_box and
320325
# _legend_box will draw itself at the location of the return
321326
# value of the find_offset.
@@ -562,10 +567,18 @@ def _init_legend_box(self, handles, labels):
562567

563568
sep = self.columnspacing*fontsize
564569

565-
self._legend_box = HPacker(pad=self.borderpad*fontsize,
566-
sep=sep, align="baseline",
567-
mode=mode,
568-
children=columnbox)
570+
self._legend_handle_box = HPacker(pad=0,
571+
sep=sep, align="baseline",
572+
mode=mode,
573+
children=columnbox)
574+
575+
self._legend_title_box = TextArea("")
576+
577+
self._legend_box = VPacker(pad=self.borderpad*fontsize,
578+
sep=self.labelspacing*fontsize,
579+
align="center",
580+
children=[self._legend_title_box,
581+
self._legend_handle_box])
569582

570583
self._legend_box.set_figure(self.figure)
571584

@@ -640,6 +653,19 @@ def get_texts(self):
640653
'return a list of text.Text instance in the legend'
641654
return silent_list('Text', self.texts)
642655

656+
def set_title(self, title):
657+
'set the legend title'
658+
self._legend_title_box._text.set_text(title)
659+
660+
if title:
661+
self._legend_title_box.set_visible(True)
662+
else:
663+
self._legend_title_box.set_visible(False)
664+
665+
def get_title(self):
666+
'return Text instance for the legend title'
667+
return self._legend_title_box._text
668+
643669
def get_window_extent(self):
644670
'return a extent of the the legend'
645671
return self.legendPatch.get_window_extent()

lib/matplotlib/offsetbox.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ def set_height(self, height):
174174
"""
175175
self.height = height
176176

177+
def get_visible_children(self):
178+
"""
179+
Return a list of visible artists it contains.
180+
"""
181+
return [c for c in self._children if c.get_visible()]
182+
177183
def get_children(self):
178184
"""
179185
Return a list of artists it contains.
@@ -208,7 +214,7 @@ def draw(self, renderer):
208214

209215
px, py = self.get_offset(width, height, xdescent, ydescent)
210216

211-
for c, (ox, oy) in zip(self.get_children(), offsets):
217+
for c, (ox, oy) in zip(self.get_visible_children(), offsets):
212218
c.set_offset((px+ox, py+oy))
213219
c.draw(renderer)
214220

@@ -281,7 +287,12 @@ def get_extent_offsets(self, renderer):
281287
pad = self.pad * dpicor
282288
sep = self.sep * dpicor
283289

284-
whd_list = [c.get_extent(renderer) for c in self.get_children()]
290+
if self.width is not None:
291+
for c in self.get_visible_children():
292+
if isinstance(c, PackerBase) and c.mode == "expand":
293+
c.set_width(self.width)
294+
295+
whd_list = [c.get_extent(renderer) for c in self.get_visible_children()]
285296
whd_list = [(w, h, xd, (h-yd)) for w, h, xd, yd in whd_list]
286297

287298

@@ -341,7 +352,7 @@ def get_extent_offsets(self, renderer):
341352
pad = self.pad * dpicor
342353
sep = self.sep * dpicor
343354

344-
whd_list = [c.get_extent(renderer) for c in self.get_children()]
355+
whd_list = [c.get_extent(renderer) for c in self.get_visible_children()]
345356

346357
if self.height is None:
347358
height_descent = max([h-yd for w,h,xd,yd in whd_list])
@@ -520,6 +531,14 @@ def __init__(self, s,
520531
self._minimumdescent = minimumdescent
521532

522533

534+
def set_text(self, s):
535+
"set text"
536+
self._text.set_text(s)
537+
538+
def get_text(self):
539+
"get text"
540+
return self._text.get_text()
541+
523542
def set_multilinebaseline(self, t):
524543
"""
525544
Set multilinebaseline .

0 commit comments

Comments
 (0)