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

Skip to content

Commit d05968d

Browse files
committed
scatterpoints support in Legend. patch by Erik Tollerud
svn path=/trunk/matplotlib/; revision=6448
1 parent 1340615 commit d05968d

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2008-11-25 Added scatterpoints support in Legend. patch by Erik
2+
Tollerud - JJL
3+
14
2008-11-24 Fix crash in log ticking. - MGD
25

36
2008-11-20 Added static helper method BrokenHBarCollection.span_where

lib/matplotlib/legend.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def __str__(self):
8383
def __init__(self, parent, handles, labels,
8484
loc = None,
8585
numpoints = None, # the number of points in the legend line
86+
scatterpoints = 3, # TODO: may be an rcParam
8687
prop = None,
8788
pad = None, # the fractional whitespace inside the legend border
8889
borderpad = None,
@@ -101,6 +102,7 @@ def __init__(self, parent, handles, labels,
101102
labels # a list of strings to label the legend
102103
loc # a location code
103104
numpoints = 4 # the number of points in the legend line
105+
scatterpoints = 3 # the number of points for the scatterplot legend
104106
prop = FontProperties(size='smaller') # the font property
105107
pad = 0.2 # the fractional whitespace inside the legend border
106108
markerscale = 0.6 # the relative size of legend markers vs. original
@@ -118,10 +120,10 @@ def __init__(self, parent, handles, labels,
118120

119121
Artist.__init__(self)
120122

121-
proplist=[numpoints, pad, borderpad, markerscale, labelsep,
123+
proplist=[numpoints, scatterpoints, pad, borderpad, markerscale, labelsep,
122124
handlelen, handletextsep, axespad, shadow]
123-
propnames=['numpoints', 'pad', 'borderpad', 'markerscale', 'labelsep',
124-
'handlelen', 'handletextsep', 'axespad', 'shadow']
125+
propnames=['numpoints','scatterpoints', 'pad', 'borderpad', 'markerscale',
126+
'labelsep', 'handlelen', 'handletextsep', 'axespad', 'shadow']
125127
for name, value in safezip(propnames,proplist):
126128
if value is None:
127129
value=rcParams["legend."+name]
@@ -130,7 +132,9 @@ def __init__(self, parent, handles, labels,
130132
warnings.warn("Use 'borderpad' instead of 'pad'.", DeprecationWarning)
131133
# 2008/10/04
132134
if self.numpoints <= 0:
133-
raise ValueError("numpoints must be >= 0; it was %d"% numpoints)
135+
raise ValueError("numpoints must be > 0; it was %d"% numpoints)
136+
if self.scatterpoints <= 0:
137+
raise ValueError("scatterpoints must be > 0; it was %d"% numpoints)
134138
if prop is None:
135139
self.prop=FontProperties(size=rcParams["legend.fontsize"])
136140
else:
@@ -142,8 +146,8 @@ def __init__(self, parent, handles, labels,
142146
self._scatteryoffsets = np.array([4./8., 5./8., 3./8.])
143147
else:
144148
self._scatteryoffsets = np.asarray(scatteryoffsets)
145-
reps = int(self.numpoints / len(self._scatteryoffsets)) + 1
146-
self._scatteryoffsets = np.tile(self._scatteryoffsets, reps)[:self.numpoints]
149+
reps = int(self.scatterpoints / len(self._scatteryoffsets)) + 1
150+
self._scatteryoffsets = np.tile(self._scatteryoffsets, reps)[:self.scatterpoints]
147151

148152
if isinstance(parent,Axes):
149153
self.isaxes = True
@@ -261,10 +265,14 @@ def _get_handles(self, handles, texts):
261265
# centered marker proxy
262266

263267
for handle, label in safezip(handles, texts):
264-
if self.numpoints > 1:
265-
xdata = np.linspace(left, left + self.handlelen, self.numpoints)
268+
if isinstance(handle, RegularPolyCollection):
269+
npoints = self.scatterpoints
270+
else:
271+
npoints = self.numpoints
272+
if npoints > 1:
273+
xdata = np.linspace(left, left + self.handlelen, npoints)
266274
xdata_marker = xdata
267-
elif self.numpoints == 1:
275+
elif npoints == 1:
268276
xdata = np.linspace(left, left + self.handlelen, 2)
269277
xdata_marker = [left + 0.5*self.handlelen]
270278

@@ -326,8 +334,11 @@ def _get_handles(self, handles, texts):
326334
# we may need to scale these sizes by "markerscale"
327335
# attribute. But other handle types does not seem
328336
# to care about this attribute and it is currently ignored.
329-
sizes = [.5*(size_max+size_min), size_max,
330-
size_min]
337+
if self.scatterpoints < 4:
338+
sizes = [.5*(size_max+size_min), size_max,
339+
size_min]
340+
else:
341+
sizes = size_max*np.linspace(0,1,self.scatterpoints)+size_min
331342

332343
p = type(handle)(handle.get_numsides(),
333344
rotation=handle.get_rotation(),

0 commit comments

Comments
 (0)