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

Skip to content

Commit bf8f8cb

Browse files
committed
Add a generic PatchCollection class.
svn path=/trunk/matplotlib/; revision=5583
1 parent 1fe36c3 commit bf8f8cb

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2008-06-17 Add a generic PatchCollection class that can contain any
2+
kind of patch. - MGD
3+
14
2008-06-13 Change pie chart label alignment to avoid having labels
25
overwrite the pie - MGD
36

examples/api/patch_collection.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import matplotlib
2+
from matplotlib.patches import Circle, Wedge, Polygon
3+
from matplotlib.collections import PatchCollection
4+
import pylab
5+
6+
fig=pylab.figure()
7+
ax=fig.add_subplot(111)
8+
9+
resolution = 50 # the number of vertices
10+
N = 3
11+
x = pylab.rand(N)
12+
y = pylab.rand(N)
13+
radii = 0.1*pylab.rand(N)
14+
patches = []
15+
for x1,y1,r in zip(x, y, radii):
16+
circle = Circle((x1,y1), r)
17+
patches.append(circle)
18+
19+
x = pylab.rand(N)
20+
y = pylab.rand(N)
21+
radii = 0.1*pylab.rand(N)
22+
theta1 = 360.0*pylab.rand(N)
23+
theta2 = 360.0*pylab.rand(N)
24+
for x1,y1,r,t1,t2 in zip(x, y, radii, theta1, theta2):
25+
wedge = Wedge((x1,y1), r, t1, t2)
26+
patches.append(wedge)
27+
28+
for i in range(N):
29+
polygon = Polygon(pylab.rand(N,2), True)
30+
patches.append(polygon)
31+
32+
colors = 100*pylab.rand(len(patches))
33+
p = PatchCollection(patches, cmap=matplotlib.cm.jet)
34+
p.set_array(pylab.array(colors))
35+
ax.add_collection(p)
36+
pylab.colorbar(p)
37+
38+
pylab.show()

lib/matplotlib/collections.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,67 @@ def get_color(self):
775775
return self._edgecolors
776776
get_colors = get_color # for compatibility with old versions
777777

778+
class PatchCollection(Collection):
779+
"""
780+
A generic collection of patches.
781+
782+
This makes it easier to assign a color map to a heterogeneous
783+
collection of patches.
784+
785+
This also may improve plotting speed, since PatchCollection will
786+
draw faster than a large number of patches.
787+
"""
788+
789+
def __init__(self, patches, match_original=False, **kwargs):
790+
"""
791+
*patches*
792+
a sequence of Patch objects. This list may include
793+
a heterogeneous assortment of different patch types.
794+
795+
*match_original* If True, use the colors and linewidths of the
796+
original patches. If False, new colors may be assigned by
797+
providing the standard collection arguments, facecolor,
798+
edgecolor, linewidths, norm or cmap.
799+
800+
If any of *edgecolors*, *facecolors*, *linewidths*,
801+
*antialiaseds* are None, they default to their
802+
:data:`matplotlib.rcParams` patch setting, in sequence form.
803+
804+
The use of :class:`~matplotlib.cm.ScalarMappable` is optional.
805+
If the :class:`~matplotlib.cm.ScalarMappable` matrix _A is not
806+
None (ie a call to set_array has been made), at draw time a
807+
call to scalar mappable will be made to set the face colors.
808+
"""
809+
810+
if match_original:
811+
def determine_facecolor(patch):
812+
if patch.fill():
813+
return patch.get_facecolor()
814+
return [0, 0, 0, 0]
815+
816+
facecolors = [determine_facecolor(p) for p in patches]
817+
edgecolors = [p.get_edgecolor() for p in patches]
818+
linewidths = [p.get_linewidths() for p in patches]
819+
antialiaseds = [p.get_antialiased() for p in patches]
820+
821+
Collection.__init__(
822+
self,
823+
edgecolors=edgecolors,
824+
facecolors=facecolors,
825+
linewidths=linewidths,
826+
linestyles='solid',
827+
antialiaseds = antialiaseds)
828+
else:
829+
Collection.__init__(self, **kwargs)
830+
831+
paths = [p.get_transform().transform_path(p.get_path())
832+
for p in patches]
833+
834+
self._paths = paths
835+
836+
def get_paths(self):
837+
return self._paths
838+
778839

779840
artist.kwdocd['Collection'] = patchstr = artist.kwdoc(Collection)
780841
for k in ('QuadMesh', 'PolyCollection', 'BrokenBarHCollection', 'RegularPolyCollection',

0 commit comments

Comments
 (0)