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

Skip to content

Commit fa3dc1f

Browse files
committed
legend supports CircleCollection.
svn path=/trunk/matplotlib/; revision=7240
1 parent 89635df commit fa3dc1f

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

lib/matplotlib/collections.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,10 @@ def __init__(self, sizes, **kwargs):
995995
self._paths = [mpath.Path.unit_circle()]
996996
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
997997

998+
def get_sizes(self):
999+
"return sizes of circles"
1000+
return self._sizes
1001+
9981002
def draw(self, renderer):
9991003
# sizes is the area of the circle circumscribing the polygon
10001004
# in points^2

lib/matplotlib/legend.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from matplotlib.font_manager import FontProperties
3232
from matplotlib.lines import Line2D
3333
from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch
34-
from matplotlib.collections import LineCollection, RegularPolyCollection
34+
from matplotlib.collections import LineCollection, RegularPolyCollection, \
35+
CircleCollection
3536
from matplotlib.transforms import Bbox, BboxBase, TransformedBbox, BboxTransformTo
3637

3738
from matplotlib.offsetbox import HPacker, VPacker, TextArea, DrawingArea
@@ -439,7 +440,8 @@ def _init_legend_box(self, handles, labels):
439440
# manually set their transform to the self.get_transform().
440441

441442
for handle in handles:
442-
if isinstance(handle, RegularPolyCollection):
443+
if isinstance(handle, RegularPolyCollection) or \
444+
isinstance(handle, CircleCollection):
443445
npoints = self.scatterpoints
444446
else:
445447
npoints = self.numpoints
@@ -531,6 +533,31 @@ def _init_legend_box(self, handles, labels):
531533
p.set_clip_path(None)
532534
handle_list.append(p)
533535

536+
elif isinstance(handle, CircleCollection):
537+
538+
ydata = height*self._scatteryoffsets
539+
540+
size_max, size_min = max(handle.get_sizes()),\
541+
min(handle.get_sizes())
542+
# we may need to scale these sizes by "markerscale"
543+
# attribute. But other handle types does not seem
544+
# to care about this attribute and it is currently ignored.
545+
if self.scatterpoints < 4:
546+
sizes = [.5*(size_max+size_min), size_max,
547+
size_min]
548+
else:
549+
sizes = (size_max-size_min)*np.linspace(0,1,self.scatterpoints)+size_min
550+
551+
p = type(handle)(sizes,
552+
offsets=zip(xdata_marker,ydata),
553+
transOffset=self.get_transform(),
554+
)
555+
556+
p.update_from(handle)
557+
p.set_figure(self.figure)
558+
p.set_clip_box(None)
559+
p.set_clip_path(None)
560+
handle_list.append(p)
534561
else:
535562
handle_list.append(None)
536563

0 commit comments

Comments
 (0)