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

Skip to content

Commit a0bed00

Browse files
committed
added rc param to control legend fancybox
svn path=/trunk/matplotlib/; revision=6511
1 parent fe5877a commit a0bed00

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

examples/pylab_examples/legend_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
ax = plt.subplot(111)
1616
plt.plot(a,c,'k--',a,d,'k:',a,c+d,'k')
1717
plt.legend(('Model length', 'Data length', 'Total message length'),
18-
'upper center', shadow=True)
18+
'upper center', shadow=True, fancybox=True)
1919
plt.ylim([-1,20])
2020
plt.grid(False)
2121
plt.xlabel('Model complexity --->')

examples/pylab_examples/legend_demo3.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
2-
2+
import matplotlib
3+
matplotlib.rcParams['legend.fancybox'] = True
34
import matplotlib.pyplot as plt
45
import numpy as np
56
import pylab
@@ -22,7 +23,7 @@ def myplot(ax):
2223

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

2728

2829
#title('Damped oscillation')

lib/matplotlib/legend.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Legend(Artist):
6363
6464
loc can be a tuple of the noramilzed coordinate values with
6565
respect its parent.
66-
66+
6767
Return value is a sequence of text, line instances that make
6868
up the legend
6969
"""
@@ -94,15 +94,15 @@ def __init__(self, parent, handles, labels,
9494
scatterpoints = 3, # TODO: may be an rcParam
9595
scatteryoffsets=None,
9696
prop = None, # properties for the legend texts
97-
97+
9898
# the following dimensions are in axes coords
9999
pad = None, # deprecated; use borderpad
100-
labelsep = None, # deprecated; use labelspacing
101-
handlelen = None, # deprecated; use handlelength
102-
handletextsep = None, # deprecated; use handletextpad
100+
labelsep = None, # deprecated; use labelspacing
101+
handlelen = None, # deprecated; use handlelength
102+
handletextsep = None, # deprecated; use handletextpad
103103
axespad = None, # deprecated; use borderaxespad
104104

105-
# spacing & pad defined as a fractionof the font-size
105+
# spacing & pad defined as a fractionof the font-size
106106
borderpad = None, # the whitespace inside the legend border
107107
labelspacing=None, #the vertical space between the legend entries
108108
handlelength=None, # the length of the legend handles
@@ -113,7 +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,
116+
fancybox=None, # True use a fancy box, false use a rounded box, none use rc
117117
shadow = None,
118118
):
119119
"""
@@ -131,7 +131,7 @@ def __init__(self, parent, handles, labels,
131131
numpoints the number of points in the legend line
132132
prop the font property
133133
markerscale the relative size of legend markers vs. original
134-
fancybox if True, draw a frame with a round fancybox.
134+
fancybox if True, draw a frame with a round fancybox. If None, use rc
135135
shadow if True, draw a shadow behind legend
136136
scatteryoffsets a list of yoffsets for scatter symbols in legend
137137
@@ -144,7 +144,7 @@ def __init__(self, parent, handles, labels,
144144
145145
The dimensions of pad and spacing are given as a fraction of the
146146
fontsize. Values from rcParams will be used if None.
147-
147+
148148
"""
149149
from matplotlib.axes import Axes # local import only to avoid circularity
150150
from matplotlib.figure import Figure # local import only to avoid circularity
@@ -172,7 +172,7 @@ def __init__(self, parent, handles, labels,
172172
# Take care the deprecated keywords
173173
deprecated_kwds = {"pad":"borderpad",
174174
"labelsep":"labelspacing",
175-
"handlelen":"handlelength",
175+
"handlelen":"handlelength",
176176
"handletextsep":"handletextpad",
177177
"axespad":"borderaxespad"}
178178

@@ -182,7 +182,7 @@ def __init__(self, parent, handles, labels,
182182
# conversion factor
183183
bbox = parent.bbox
184184
axessize_fontsize = min(bbox.width, bbox.height)/self.fontsize
185-
185+
186186
for k, v in deprecated_kwds.items():
187187
# use deprecated value if not None and if their newer
188188
# counter part is None.
@@ -199,7 +199,7 @@ def __init__(self, parent, handles, labels,
199199
setattr(self, v, localdict[v])
200200

201201
del localdict
202-
202+
203203
self._ncol = ncol
204204

205205
if self.numpoints <= 0:
@@ -265,6 +265,9 @@ def __init__(self, parent, handles, labels,
265265
# The width and height of the legendPatch will be set (in the
266266
# draw()) to the length that includes the padding. Thus we set
267267
# pad=0 here.
268+
if fancybox is None:
269+
fancybox = rcParams["legend.fancybox"]
270+
268271
if fancybox == True:
269272
self.legendPatch.set_boxstyle("round",pad=0,
270273
rounding_size=0.2)
@@ -318,7 +321,7 @@ def draw(self, renderer):
318321

319322
# find_offset function will be provided to _legend_box and
320323
# _legend_box will draw itself at the location of the return
321-
# value of the find_offset.
324+
# value of the find_offset.
322325
if self._loc == 0:
323326
self._legend_box.set_offset(self._findoffset_best)
324327
else:
@@ -339,7 +342,7 @@ def draw(self, renderer):
339342
if self.shadow:
340343
shadow = Shadow(self.legendPatch, 2, -2)
341344
shadow.draw(renderer)
342-
345+
343346
self.legendPatch.draw(renderer)
344347

345348
self._legend_box.draw(renderer)
@@ -360,7 +363,7 @@ def _init_legend_box(self, handles, labels):
360363
Initiallize the legend_box. The legend_box is an instance of
361364
the OffsetBox, which is packed with legend handles and
362365
texts. Once packed, their location is calculated during the
363-
drawing time.
366+
drawing time.
364367
"""
365368

366369
# legend_box is a HPacker, horizontally packed with
@@ -371,7 +374,7 @@ def _init_legend_box(self, handles, labels):
371374
# is an instance of offsetbox.TextArea which contains legend
372375
# text.
373376

374-
377+
375378
text_list = [] # the list of text instances
376379
handle_list = [] # the list of text instances
377380

@@ -394,12 +397,12 @@ def _init_legend_box(self, handles, labels):
394397
# The approximate height and descent of text. These values are
395398
# only used for plotting the legend handle.
396399
height = self._approx_text_height() * 0.7
397-
descent = 0.
400+
descent = 0.
398401

399402
# each handle needs to be drawn inside a box of
400403
# (x, y, w, h) = (0, -descent, width, height).
401404
# And their corrdinates should be given in the display coordinates.
402-
405+
403406
# The transformation of each handle will be automatically set
404407
# to self.get_trasnform(). If the artist does not uses its
405408
# default trasnform (eg, Collections), you need to
@@ -413,7 +416,7 @@ def _init_legend_box(self, handles, labels):
413416
if npoints > 1:
414417
# we put some pad here to compensate the size of the
415418
# marker
416-
xdata = np.linspace(0.3*self.fontsize,
419+
xdata = np.linspace(0.3*self.fontsize,
417420
(self.handlelength-0.3)*self.fontsize,
418421
npoints)
419422
xdata_marker = xdata
@@ -484,14 +487,14 @@ def _init_legend_box(self, handles, labels):
484487
size_min]
485488
else:
486489
sizes = (size_max-size_min)*np.linspace(0,1,self.scatterpoints)+size_min
487-
490+
488491
p = type(handle)(handle.get_numsides(),
489492
rotation=handle.get_rotation(),
490493
sizes=sizes,
491494
offsets=zip(xdata_marker,ydata),
492495
transOffset=self.get_transform(),
493496
)
494-
497+
495498
p.update_from(handle)
496499
p.set_figure(self.figure)
497500
p.set_clip_box(None)
@@ -534,7 +537,7 @@ def _init_legend_box(self, handles, labels):
534537
for h, t in handle_label[i0:i0+di]]
535538
# minimumdescent=False for the text of the last row of the column
536539
itemBoxes[-1].get_children()[1].set_minimumdescent(False)
537-
540+
538541
# pack columnBox
539542
columnbox.append(VPacker(pad=0,
540543
sep=self.labelspacing*self.fontsize,
@@ -547,15 +550,15 @@ def _init_legend_box(self, handles, labels):
547550
mode = "fixed"
548551

549552
sep = self.columnspacing*self.fontsize
550-
553+
551554
self._legend_box = HPacker(pad=self.borderpad*self.fontsize,
552555
sep=sep, align="baseline",
553556
mode=mode,
554557
children=columnbox)
555558

556559
self.texts = text_list
557560
self.legendHandles = handle_list
558-
561+
559562

560563
def _auto_legend_data(self):
561564
"""
@@ -655,12 +658,12 @@ def _get_anchored_bbox(self, loc, bbox, parentbbox):
655658
LC:"S",
656659
UC:"N",
657660
C:"C"}
658-
661+
659662
c = anchor_coefs[loc]
660663

661664
container = parentbbox.padded(-(self.borderaxespad) * self.fontsize)
662665
anchored_box = bbox.anchored(c, container=container)
663-
return anchored_box.x0, anchored_box.y0
666+
return anchored_box.x0, anchored_box.y0
664667

665668

666669
def _find_best_position(self, width, height, consider=None):

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ def __call__(self, s):
417417
'polaraxes.grid' : [True, validate_bool], # display polar grid or not
418418

419419
#legend properties
420+
'legend.fancybox' : [False,validate_bool],
420421
'legend.loc' : ['upper right',validate_legend_loc], # at some point, this should be changed to 'best'
421422
'legend.isaxes' : [True,validate_bool], # this option is internally ignored - it never served any useful purpose
422423
'legend.numpoints' : [2, validate_int], # the number of points in the legend line

matplotlibrc.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ numerix : %(numerix)s # numpy, Numeric or numarray
235235
#grid.linewidth : 0.5 # in points
236236

237237
### Legend
238+
#legend.fancybox : False # if True, use a rounded box for the
239+
# legend, else a rectangle
238240
#legend.isaxes : True
239241
#legend.numpoints : 2 # the number of points in the legend line
240242
#legend.fontsize : large

0 commit comments

Comments
 (0)