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

Skip to content

Commit d8e8a65

Browse files
author
pkienzle
committed
patches: allow partial rings with Wedge
svn path=/trunk/matplotlib/; revision=6404
1 parent 2fa3397 commit d8e8a65

2 files changed

Lines changed: 47 additions & 16 deletions

File tree

examples/api/patch_collection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@
2525
wedge = Wedge((x1,y1), r, t1, t2)
2626
patches.append(wedge)
2727

28+
# Some limiting conditions on Wedge
29+
patches += [
30+
Wedge((.3,.7), .1, 0, 360), # Full circle
31+
Wedge((.7,.8), .2, 0, 360, width=0.05), # Full ring
32+
Wedge((.8,.3), .2, 0, 45), # Full sector
33+
Wedge((.8,.3), .2, 45, 90, width=0.10), # Ring sector
34+
]
35+
2836
for i in range(N):
2937
polygon = Polygon(pylab.rand(N,2), True)
3038
patches.append(polygon)
3139

3240
colors = 100*pylab.rand(len(patches))
33-
p = PatchCollection(patches, cmap=matplotlib.cm.jet)
41+
p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
3442
p.set_array(pylab.array(colors))
3543
ax.add_collection(p)
3644
pylab.colorbar(p)

lib/matplotlib/patches.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -712,38 +712,62 @@ def set_xy(self, vertices):
712712
:meth:`~matplotlib.patches.Polygon.set_xy` instead.""")
713713

714714
class Wedge(Patch):
715+
"""
716+
Wedge shaped patch.
717+
"""
715718
def __str__(self):
716719
return "Wedge(%g,%g)"%(self.theta1,self.theta2)
717720

718-
def __init__(self, center, r, theta1, theta2, **kwargs):
721+
def __init__(self, center, r, theta1, theta2, width=0, **kwargs):
719722
"""
720723
Draw a wedge centered at *x*, *y* center with radius *r* that
721-
sweeps *theta1* to *theta2* (in degrees).
724+
sweeps *theta1* to *theta2* (in degrees). If *width* is given,
725+
then a partial wedge is drawn from inner radius *r* - *width*
726+
to outer radius *r*.
722727
723728
Valid kwargs are:
724729
725730
%(Patch)s
726731
"""
727732
Patch.__init__(self, **kwargs)
728733
self.center = center
729-
self.r = r
730-
self.theta1 = theta1
731-
self.theta2 = theta2
734+
self.r,self.width = r,width
735+
self.theta1,self.theta2 = theta1,theta2
736+
737+
# Inner and outer rings are connected unless the annulus is complete
738+
delta=theta2-theta1
739+
if abs((theta2-theta1) - 360) <= 1e-12:
740+
theta1,theta2 = 0,360
741+
connector = Path.MOVETO
742+
else:
743+
connector = Path.LINETO
744+
745+
# Form the outer ring
746+
arc = Path.arc(theta1,theta2)
747+
748+
if width != 0:
749+
# Partial annulus needs to draw the outter ring
750+
# followed by a reversed and scaled inner ring
751+
v1 = arc.vertices
752+
v2 = arc.vertices[::-1]*float(r-width)/r
753+
v = np.vstack([v1,v2,v1[0,:],(0,0)])
754+
c = np.hstack([arc.codes,arc.codes,connector,Path.CLOSEPOLY])
755+
c[len(arc.codes)]=connector
756+
else:
757+
# Wedge doesn't need an inner ring
758+
v = np.vstack([arc.vertices,[(0,0),arc.vertices[0,:],(0,0)]])
759+
c = np.hstack([arc.codes,[connector,connector,Path.CLOSEPOLY]])
760+
761+
# Shift and scale the wedge to the final location.
762+
v *= r
763+
v += np.asarray(center)
764+
self._path = Path(v,c)
732765
self._patch_transform = transforms.IdentityTransform()
733-
self._path = Path.wedge(self.theta1, self.theta2)
734766
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
735767

736768
def get_path(self):
737769
return self._path
738770

739-
def get_patch_transform(self):
740-
x = self.convert_xunits(self.center[0])
741-
y = self.convert_yunits(self.center[1])
742-
rx = self.convert_xunits(self.r)
743-
ry = self.convert_yunits(self.r)
744-
self._patch_transform = transforms.Affine2D() \
745-
.scale(rx, ry).translate(x, y)
746-
return self._patch_transform
747771

748772
# COVERAGE NOTE: Not used internally or from examples
749773
class Arrow(Patch):
@@ -3042,4 +3066,3 @@ def draw(self, renderer):
30423066
self.fill = fill_orig
30433067

30443068
#renderer.close_group('patch')
3045-

0 commit comments

Comments
 (0)