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

Skip to content

Commit dba51b2

Browse files
committed
added configurable option legend.loc
svn path=/trunk/matplotlib/; revision=3506
1 parent dbbdfcb commit dba51b2

6 files changed

Lines changed: 123 additions & 102 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2007-07-13 Added legend.loc as configurable option that could in future default to 'best'. - NN
2+
13
2007-07-12 Bugfixes in mlab.py to coerce inputs into numpy arrays. -ADS
24

35
2007-07-11 Added linespacing kwarg to text.Text - EF

examples/legend_auto.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pylab import *
1010
import sys
1111

12+
rcParams['legend.loc'] = 'best'
1213

1314
N = 100
1415
x = arange(N)
@@ -17,55 +18,55 @@ def fig_1():
1718
figure(1)
1819
t = arange(0, 40.0 * pi, 0.1)
1920
l, = plot(t, 100*sin(t), 'r', label='sine')
20-
legend(loc='best')
21+
legend()
2122

2223
def fig_2():
2324
figure(2)
2425
plot(x, 'o', label='x=y')
25-
legend(loc='best')
26+
legend()
2627

2728
def fig_3():
2829
figure(3)
2930
plot(x, -x, 'o', label='x= -y')
30-
legend(loc='best')
31+
legend()
3132

3233
def fig_4():
3334
figure(4)
3435
plot(x, ones(len(x)), 'o', label='y=1')
3536
plot(x, -ones(len(x)), 'o', label='y=-1')
36-
legend(loc='best')
37+
legend()
3738

3839
def fig_5():
3940
figure(5)
4041
n, bins, patches = hist(randn(1000), 40, normed=1)
4142
l, = plot(bins, normpdf(bins, 0.0, 1.0), 'r--', label='fit', linewidth=3)
42-
legend([l, patches[0]], ['fit', 'hist'], loc='best')
43+
legend([l, patches[0]], ['fit', 'hist'])
4344

4445
def fig_6():
4546
figure(6)
4647
plot(x, 50-x, 'o', label='y=1')
4748
plot(x, x-50, 'o', label='y=-1')
48-
legend(loc='best')
49+
legend()
4950

5051
def fig_7():
5152
figure(7)
5253
xx = x - (N/2.0)
5354
plot(xx, (xx*xx)-1225, 'bo', label='$y=x^2$')
5455
plot(xx, 25*xx, 'go', label='$y=25x$')
5556
plot(xx, -25*xx, 'mo', label='$y=-25x$')
56-
legend(loc='best')
57+
legend()
5758

5859
def fig_8():
5960
figure(8)
6061
b1 = bar(x, x, color='m')
6162
b2 = bar(x, x[::-1], color='g')
62-
legend([b1[0], b2[0]], ['up', 'down'], loc='best')
63+
legend([b1[0], b2[0]], ['up', 'down'])
6364

6465
def fig_9():
6566
figure(9)
6667
b1 = bar(x, -x)
6768
b2 = bar(x, -x[::-1], color='r')
68-
legend([b1[0], b2[0]], ['down', 'up'], loc='best')
69+
legend([b1[0], b2[0]], ['down', 'up'])
6970

7071
def fig_10():
7172
figure(10)
@@ -74,8 +75,7 @@ def fig_10():
7475
b3 = bar(x, -x, bottom=100)
7576
b4 = bar(x, -x[::-1], bottom=100, color='r')
7677
legend([b1[0], b2[0], b3[0], b4[0]], ['bottom right', 'bottom left',
77-
'top left', 'top right'],
78-
loc='best')
78+
'top left', 'top right'])
7979

8080
if __name__ == '__main__':
8181
nfigs = 10

lib/matplotlib/axes.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,8 +2913,8 @@ def legend(self, *args, **kwargs):
29132913
29142914
The location codes are
29152915
2916-
'best' : 0,
2917-
'upper right' : 1, (default)
2916+
'best' : 0,
2917+
'upper right' : 1,
29182918
'upper left' : 2,
29192919
'lower left' : 3,
29202920
'lower right' : 4,
@@ -2952,7 +2952,6 @@ def get_handles():
29522952
handles.extend([c for c in self.collections if isinstance(c, RegularPolyCollection)])
29532953
return handles
29542954

2955-
29562955
if len(args)==0:
29572956
handles = []
29582957
labels = []
@@ -2961,33 +2960,32 @@ def get_handles():
29612960
if label is not None and label != '' and not label.startswith('_'):
29622961
handles.append(handle)
29632962
labels.append(label)
2964-
loc = popd(kwargs, 'loc', 1)
29652963

29662964
elif len(args)==1:
29672965
# LABELS
29682966
labels = args[0]
29692967
handles = [h for h, label in zip(get_handles(), labels)]
2970-
loc = popd(kwargs, 'loc', 1)
29712968

29722969
elif len(args)==2:
29732970
if is_string_like(args[1]) or isinstance(args[1], int):
29742971
# LABELS, LOC
29752972
labels, loc = args
29762973
handles = [h for h, label in zip(get_handles(), labels)]
2974+
kwargs['loc'] = loc
29772975
else:
29782976
# LINES, LABELS
29792977
handles, labels = args
2980-
loc = popd(kwargs, 'loc', 1)
29812978

29822979
elif len(args)==3:
29832980
# LINES, LABELS, LOC
29842981
handles, labels, loc = args
2982+
kwargs['loc'] = loc
29852983
else:
29862984
raise TypeError('Invalid arguments to legend')
29872985

29882986

29892987
handles = flatten(handles)
2990-
self.legend_ = Legend(self, handles, labels, loc, **kwargs)
2988+
self.legend_ = Legend(self, handles, labels, **kwargs)
29912989
return self.legend_
29922990

29932991

lib/matplotlib/figure.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,16 @@ def get_children(self):
200200
return children
201201

202202
def contains(self, mouseevent):
203-
"""Test whether the mouse event occurred on the figure.
204-
203+
"""Test whether the mouse event occurred on the figure.
204+
205205
Returns True,{}
206206
"""
207207
if callable(self._contains): return self._contains(self,mouseevent)
208208
#inside = mouseevent.x >= 0 and mouseevent.y >= 0
209209
inside = self.bbox.contains(mouseevent.x,mouseevent.y)
210-
210+
211211
return inside,{}
212-
212+
213213
def get_window_extent(self, *args, **kwargs):
214214
'get the figure bounding box in display space; kwargs are void'
215215
return self.bbox
@@ -631,7 +631,7 @@ def draw_artist(self, a):
631631
def get_axes(self):
632632
return self.axes
633633

634-
def legend(self, handles, labels, loc, **kwargs):
634+
def legend(self, handles, labels, *args, **kwargs):
635635
"""
636636
Place a legend in the figure. Labels are a sequence of
637637
strings, handles is a sequence of line or patch instances, and
@@ -645,8 +645,8 @@ def legend(self, handles, labels, loc, **kwargs):
645645
646646
The LOC location codes are
647647
648-
'best' : 0, (currently not supported, defaults to upper right)
649-
'upper right' : 1, (default)
648+
'best' : 0, (currently not supported for figure legends)
649+
'upper right' : 1,
650650
'upper left' : 2,
651651
'lower left' : 3,
652652
'lower right' : 4,
@@ -664,7 +664,7 @@ def legend(self, handles, labels, loc, **kwargs):
664664
665665
The legend instance is returned. The following kwargs are supported:
666666
667-
isaxes=True # whether this is an axes legend
667+
loc = "upper right" #
668668
numpoints = 4 # the number of points in the legend line
669669
prop = FontProperties(size='smaller') # the font property
670670
pad = 0.2 # the fractional whitespace inside the legend border
@@ -679,7 +679,7 @@ def legend(self, handles, labels, loc, **kwargs):
679679

680680

681681
handles = flatten(handles)
682-
l = Legend(self, handles, labels, loc, isaxes=False, **kwargs)
682+
l = Legend(self, handles, labels, *args, **kwargs)
683683
self._set_artist_props(l)
684684
self.legends.append(l)
685685
return l

0 commit comments

Comments
 (0)