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

Skip to content

Commit c6e579d

Browse files
committed
some minor changes for the legend
svn path=/trunk/matplotlib/; revision=6494
1 parent cf3211f commit c6e579d

4 files changed

Lines changed: 104 additions & 27 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2008-12-04 Added fancybox keyword to legend. Also applied some changes
2+
for better look, including baseline adjustment of the
3+
multiline texts so that it is center aligned. -JJL
4+
15
2008-12-02 The transmuter classes in the patches.py are reorganized as
26
subclasses of the Style classes. A few more box and arrow
37
styles are added. -JJL

examples/pylab_examples/legend_demo3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import matplotlib.pyplot as plt
44
import numpy as np
5+
import pylab
56

67
def myplot(ax):
78
t1 = np.arange(0.0, 1.0, 0.1)
@@ -21,7 +22,7 @@ def myplot(ax):
2122

2223
ax3 = plt.subplot(3,1,3)
2324
myplot(ax3)
24-
ax3.legend(loc=1, ncol=4, mode="expand", shadow=True)
25+
ax3.legend(loc=1, ncol=4, mode="expand", fancybox=False, shadow=True)
2526

2627

2728
#title('Damped oscillation')

lib/matplotlib/legend.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def __init__(self, parent, handles, labels,
113113
ncol=1, # number of columns
114114
mode=None, # mode for horizontal distribution of columns. None, "expand"
115115

116+
fancybox=True,
116117
shadow = None,
117118
):
118119
"""
@@ -130,6 +131,7 @@ def __init__(self, parent, handles, labels,
130131
numpoints the number of points in the legend line
131132
prop the font property
132133
markerscale the relative size of legend markers vs. original
134+
fancybox if True, draw a frame with a round fancybox.
133135
shadow if True, draw a shadow behind legend
134136
scatteryoffsets a list of yoffsets for scatter symbols in legend
135137
@@ -263,8 +265,11 @@ def __init__(self, parent, handles, labels,
263265
# The width and height of the legendPatch will be set (in the
264266
# draw()) to the length that includes the padding. Thus we set
265267
# pad=0 here.
266-
self.legendPatch.set_boxstyle("round",pad=0, #self.borderpad,
267-
rounding_size=0.2)
268+
if fancybox == True:
269+
self.legendPatch.set_boxstyle("round",pad=0,
270+
rounding_size=0.2)
271+
else:
272+
self.legendPatch.set_boxstyle("square",pad=0)
268273

269274
self._set_artist_props(self.legendPatch)
270275

@@ -378,7 +383,8 @@ def _init_legend_box(self, handles, labels):
378383
labelboxes = []
379384

380385
for l in labels:
381-
textbox = TextArea(l, textprops=label_prop)
386+
textbox = TextArea(l, textprops=label_prop
387+
multilinebaseline=True, minimumdescent=True)
382388
text_list.append(textbox._text)
383389
labelboxes.append(textbox)
384390

@@ -387,8 +393,8 @@ def _init_legend_box(self, handles, labels):
387393

388394
# The approximate height and descent of text. These values are
389395
# only used for plotting the legend handle.
390-
height = self._approx_text_height() * 0.6
391-
descent = 0. #height/6.
396+
height = self._approx_text_height() * 0.7
397+
descent = 0.
392398

393399
# each handle needs to be drawn inside a box of
394400
# (x, y, w, h) = (0, -descent, width, height).
@@ -440,9 +446,9 @@ def _init_legend_box(self, handles, labels):
440446
legline._legmarker = legline_marker
441447

442448
elif isinstance(handle, Patch):
443-
p = Rectangle(xy=(0, -0.*descent),
449+
p = Rectangle(xy=(0., 0.),
444450
width = self.handlelength*self.fontsize,
445-
height=0.*descent+(height-descent)*.9,
451+
height=(height-descent),
446452
)
447453
p.update_from(handle)
448454
self._set_artist_props(p)
@@ -513,19 +519,21 @@ def _init_legend_box(self, handles, labels):
513519
num_smallcol = self._ncol-num_largecol
514520

515521
# starting index of each column and number of rows in it.
516-
largecol = zip(range(0, num_largecol*(nrows+1), (nrows+1)),
517-
[nrows+1] * num_largecol)
518-
smallcol = zip(range(num_largecol*(nrows+1), len(handleboxes), nrows),
519-
[nrows] * num_smallcol)
522+
largecol = safezip(range(0, num_largecol*(nrows+1), (nrows+1)),
523+
[nrows+1] * num_largecol)
524+
smallcol = safezip(range(num_largecol*(nrows+1), len(handleboxes), nrows),
525+
[nrows] * num_smallcol)
520526

521-
handle_label = zip(handleboxes, labelboxes)
527+
handle_label = safezip(handleboxes, labelboxes)
522528
columnbox = []
523529
for i0, di in largecol+smallcol:
524530
# pack handleBox and labelBox into itemBox
525531
itemBoxes = [HPacker(pad=0,
526532
sep=self.handletextpad*self.fontsize,
527533
children=[h, t], align="baseline")
528534
for h, t in handle_label[i0:i0+di]]
535+
# minimumdescent=False for the text of the last row of the column
536+
itemBoxes[-1].get_children()[1].set_minimumdescent(False)
529537

530538
# pack columnBox
531539
columnbox.append(VPacker(pad=0,

lib/matplotlib/offsetbox.py

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
from matplotlib.patches import bbox_artist as mbbox_artist
2424
DEBUG=False
2525
# for debuging use
26-
def bbox_artist(*kl, **kw):
26+
def bbox_artist(*args, **kwargs):
2727
if DEBUG:
28-
mbbox_artist(*kl, **kw)
28+
mbbox_artist(*args, **kwargs)
2929

3030

3131
# _get_packed_offsets() and _get_aligned_offsets() are coded assuming
@@ -122,9 +122,9 @@ class OffsetBox(martist.Artist):
122122
The OffsetBox is a simple container artist. The child artist are meant
123123
to be drawn at a relative position to its parent.
124124
"""
125-
def __init__(self, *kl, **kw):
125+
def __init__(self, *args, **kwargs):
126126

127-
super(OffsetBox, self).__init__(*kl, **kw)
127+
super(OffsetBox, self).__init__(*args, **kwargs)
128128

129129
self._children = []
130130
self._offset = (0, 0)
@@ -441,10 +441,18 @@ class TextArea(OffsetBox):
441441

442442

443443

444-
def __init__(self, s, textprops=None, **kw):
445-
"""
446-
*s* : a string to be displayer.
447-
*trnaspose* : transformation matrrix
444+
def __init__(self, s,
445+
textprops=None,
446+
multilinebaseline=None,
447+
minimumdescent=True,
448+
):
449+
"""
450+
*s* : a string to be displayed.
451+
*textprops* : property dictionary for the text
452+
*multilinebaseline* : If True, baseline for multiline text is
453+
adjusted so that it is (approximatedly)
454+
center-aligned with singleline text.
455+
*minimumdescent* : If True, the box has a minimum descent of "p".
448456
"""
449457
if textprops is None:
450458
textprops = {}
@@ -462,7 +470,46 @@ def __init__(self, s, textprops=None, **kw):
462470
self.offset_transform = mtransforms.Affine2D()
463471
self.offset_transform.clear()
464472
self.offset_transform.translate(0, 0)
465-
self._text.set_transform(self.offset_transform)
473+
self._baseline_transform = mtransforms.Affine2D()
474+
self._text.set_transform(self.offset_transform+self._baseline_transform)
475+
476+
self._multilinebaseline = multilinebaseline
477+
self._minimumdescent = minimumdescent
478+
479+
480+
def set_multilinebaseline(self, t):
481+
"""
482+
Set multilinebaseline .
483+
484+
If True, baseline for multiline text is
485+
adjusted so that it is (approximatedly) center-aligned with
486+
singleline text.
487+
"""
488+
self._multilinebaseline = t
489+
490+
491+
def get_multilinebaseline(self):
492+
"""
493+
get multilinebaseline .
494+
"""
495+
return self._multilinebaseline
496+
497+
498+
def set_minimumdescent(self, t):
499+
"""
500+
Set minimumdescent .
501+
502+
If True, extent of the single line text is adjusted so that
503+
it has minimum descent of "p"
504+
"""
505+
self._minimumdescent = t
506+
507+
508+
def get_minimumdescent(self):
509+
"""
510+
get minimumdescent.
511+
"""
512+
return self._minimumdescent
466513

467514

468515
def set_transform(self, t):
@@ -507,17 +554,34 @@ def get_extent(self, renderer):
507554

508555
bbox, info = self._text._get_layout(renderer)
509556
w, h = bbox.width, bbox.height
557+
510558
line = info[0][0] # first line
511559

512560
_, hh, dd = renderer.get_text_width_height_descent(
513561
line, self._text._fontproperties, ismath=ismath)
514-
d = h-(hh-dd) # the baseline of the first line
515562

516-
# for multiple lines, h or d may greater than h_ or d_.
517-
h_d = max(h_ - d_, h-d)
518-
d = max(d, d_)
519-
h = h_d + d
520563

564+
self._baseline_transform.clear()
565+
if len(info) > 1 and self._multilinebaseline: # multi line
566+
d = h-(hh-dd) # the baseline of the first line
567+
d_new = 0.5 * h - 0.5 * (h_ - d_)
568+
569+
self._baseline_transform.translate(0, d - d_new)
570+
d = d_new
571+
572+
else: # single line
573+
574+
h_d = max(h_ - d_, h-dd)
575+
576+
if self.get_minimumdescent():
577+
## to have a minimum descent, #i.e., "l" and "p" have same
578+
## descents.
579+
d = max(dd, d_)
580+
else:
581+
d = dd
582+
583+
h = h_d + d
584+
521585
return w, h, 0., d
522586

523587

0 commit comments

Comments
 (0)