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

Skip to content

Commit f415347

Browse files
committed
use a class helper method to make the compound path from polys
svn path=/branches/v0_99_maint/; revision=7431
1 parent 6433e8e commit f415347

3 files changed

Lines changed: 56 additions & 30 deletions

File tree

examples/animation/histogram_tkagg.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
This example shows how to use a path patch to draw a bunch of
33
rectangles for an animated histogram
44
"""
5+
import time
56
import numpy as np
67
import matplotlib
78
matplotlib.use('TkAgg') # do this before importing pylab
@@ -52,18 +53,18 @@
5253
ax.set_ylim(bottom.min(), top.max())
5354

5455
def animate():
56+
if animate.cnt>=100:
57+
return
58+
59+
animate.cnt += 1
5560
# simulate new data coming in
5661
data = np.random.randn(1000)
5762
n, bins = np.histogram(data, 100)
5863
top = bottom + n
5964
verts[1::5,1] = top
6065
verts[2::5,1] = top
6166
fig.canvas.draw()
62-
63-
def run():
64-
for i in range(100):
65-
fig.canvas.manager.window.after(100, animate)
66-
67-
68-
fig.canvas.manager.window.after(100, run)
67+
fig.canvas.manager.window.after(100, animate)
68+
animate.cnt = 0
69+
fig.canvas.manager.window.after(100, animate)
6970
plt.show()

examples/api/histogram_path_demo.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
# histogram our data with numpy
2222
data = np.random.randn(1000)
23-
n, bins = np.histogram(data, 100)
23+
n, bins = np.histogram(data, 50)
24+
2425

2526
# get the corners of the rectangles for the histogram
2627
left = np.array(bins[:-1])
@@ -29,28 +30,24 @@
2930
top = bottom + n
3031
nrects = len(left)
3132

32-
# here comes the tricky part -- we have to set up the vertex and path
33-
# codes arrays using moveto, lineto and closepoly
34-
35-
# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
36-
# CLOSEPOLY; the vert for the closepoly is ignored but we still need
37-
# it to keep the codes aligned with the vertices
38-
nverts = nrects*(1+3+1)
39-
verts = np.zeros((nverts, 2))
40-
codes = np.ones(nverts, int) * path.Path.LINETO
41-
codes[0::5] = path.Path.MOVETO
42-
codes[4::5] = path.Path.CLOSEPOLY
43-
verts[0::5,0] = left
44-
verts[0::5,1] = bottom
45-
verts[1::5,0] = left
46-
verts[1::5,1] = top
47-
verts[2::5,0] = right
48-
verts[2::5,1] = top
49-
verts[3::5,0] = right
50-
verts[3::5,1] = bottom
51-
52-
barpath = path.Path(verts, codes)
53-
patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
33+
XY = np.zeros((nrects, 4, 2))
34+
XY[:,0,0] = left
35+
XY[:,0,1] = bottom
36+
37+
XY[:,1,0] = left
38+
XY[:,1,1] = top
39+
40+
XY[:,2,0] = right
41+
XY[:,2,1] = top
42+
43+
XY[:,3,0] = right
44+
XY[:,3,1] = bottom
45+
46+
47+
48+
barpath = path.Path.make_compound_path_from_polys(XY)
49+
print barpath.codes[:7], barpath.codes[-7:]
50+
patch = patches.PathPatch(barpath, facecolor='blue', edgecolor='gray', alpha=0.8)
5451
ax.add_patch(patch)
5552

5653
ax.set_xlim(left[0], right[-1])

lib/matplotlib/path.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1):
128128
self.vertices = vertices
129129
self._interpolation_steps = _interpolation_steps
130130

131+
@classmethod
132+
def make_compound_path_from_polys(cls, XY):
133+
"""
134+
(static method) Make a compound path object to draw a number
135+
of polygons with equal numbers of sides XY is a (numpolys x
136+
numsides x 2) numpy array of vertices. Return object is a
137+
:class:`Path`
138+
139+
.. plot:: mpl_examples/api/histogram_path_demo.py
140+
141+
"""
142+
143+
# for each poly: 1 for the MOVETO, (numsides-1) for the LINETO, 1 for the
144+
# CLOSEPOLY; the vert for the closepoly is ignored but we still need
145+
# it to keep the codes aligned with the vertices
146+
numpolys, numsides, two = XY.shape
147+
assert(two==2)
148+
stride = numsides + 1
149+
nverts = numpolys * stride
150+
verts = np.zeros((nverts, 2))
151+
codes = np.ones(nverts, int) * cls.LINETO
152+
codes[0::stride] = cls.MOVETO
153+
codes[numsides::stride] = cls.CLOSEPOLY
154+
for i in range(numsides):
155+
verts[i::stride] = XY[:,i]
156+
157+
return cls(verts, codes)
158+
131159
@classmethod
132160
def make_compound_path(cls, *args):
133161
"""

0 commit comments

Comments
 (0)