Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cd88e29

Browse filesBrowse files
committed
two new examples using a compund path for a histogram; one animated
svn path=/branches/v0_99_maint/; revision=7429
1 parent aca89b1 commit cd88e29
Copy full SHA for cd88e29

File tree

Expand file treeCollapse file tree

2 files changed

+137
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+137
-0
lines changed

‎examples/animation/histogram_tkagg.py

Copy file name to clipboard
+78Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
This example shows how to use a path patch to draw a bunch of
3+
rectangles. The technique of using lots of Rectangle instances, or
4+
the faster method of using PolyCollections, were implemented before we
5+
had proper paths with moveto/lineto, closepoly etc in mpl. Now that
6+
we have them, we can draw collections of regularly shaped objects with
7+
homogeous properties more efficiently with a PathCollection. This
8+
example makes a histogram -- its more work to set up the vertex arrays
9+
at the outset, but it should be much faster for large numbers of
10+
objects
11+
"""
12+
import time
13+
import numpy as np
14+
import matplotlib
15+
matplotlib.use('TkAgg') # do this before importing pylab
16+
17+
import matplotlib.pyplot as plt
18+
import matplotlib.patches as patches
19+
import matplotlib.path as path
20+
21+
fig = plt.figure()
22+
ax = fig.add_subplot(111)
23+
24+
# histogram our data with numpy
25+
data = np.random.randn(1000)
26+
n, bins = np.histogram(data, 100)
27+
28+
# get the corners of the rectangles for the histogram
29+
left = np.array(bins[:-1])
30+
right = np.array(bins[1:])
31+
bottom = np.zeros(len(left))
32+
top = bottom + n
33+
nrects = len(left)
34+
35+
# here comes the tricky part -- we have to set up the vertex and path
36+
# codes arrays using moveto, lineto and closepoly
37+
38+
# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
39+
# CLOSEPOLY; the vert for the closepoly is ignored but we still need
40+
# it to keep the codes aligned with the vertices
41+
nverts = nrects*(1+3+1)
42+
verts = np.zeros((nverts, 2))
43+
codes = np.ones(nverts, int) * path.Path.LINETO
44+
codes[0::5] = path.Path.MOVETO
45+
codes[4::5] = path.Path.CLOSEPOLY
46+
verts[0::5,0] = left
47+
verts[0::5,1] = bottom
48+
verts[1::5,0] = left
49+
verts[1::5,1] = top
50+
verts[2::5,0] = right
51+
verts[2::5,1] = top
52+
verts[3::5,0] = right
53+
verts[3::5,1] = bottom
54+
55+
barpath = path.Path(verts, codes)
56+
patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
57+
ax.add_patch(patch)
58+
59+
ax.set_xlim(left[0], right[-1])
60+
ax.set_ylim(bottom.min(), top.max())
61+
62+
def animate():
63+
tstart = time.time() # for profiling
64+
# simulate new data coming in
65+
data = np.random.randn(1000)
66+
n, bins = np.histogram(data, 100)
67+
top = bottom + n
68+
verts[1::5,1] = top
69+
verts[2::5,1] = top
70+
fig.canvas.draw()
71+
72+
def run():
73+
for i in range(100):
74+
fig.canvas.manager.window.after(100, animate)
75+
76+
77+
fig.canvas.manager.window.after(100, run)
78+
plt.show()

‎examples/api/histogram_path_demo.py

Copy file name to clipboard
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
This example shows how to use a path patch to draw a bunch of
3+
rectangles. The technique of using lots of Rectangle instances, or
4+
the faster method of using PolyCollections, were implemented before we
5+
had proper paths with moveto/lineto, closepoly etc in mpl. Now that
6+
we have them, we can draw collections of regularly shaped objects with
7+
homogeous properties more efficiently with a PathCollection. This
8+
example makes a histogram -- its more work to set up the vertex arrays
9+
at the outset, but it should be much faster for large numbers of
10+
objects
11+
"""
12+
13+
import numpy as np
14+
import matplotlib.pyplot as plt
15+
import matplotlib.patches as patches
16+
import matplotlib.path as path
17+
18+
fig = plt.figure()
19+
ax = fig.add_subplot(111)
20+
21+
# histogram our data with numpy
22+
data = np.random.randn(1000)
23+
n, bins = np.histogram(data, 100)
24+
25+
# get the corners of the rectangles for the histogram
26+
left = np.array(bins[:-1])
27+
right = np.array(bins[1:])
28+
bottom = np.zeros(len(left))
29+
top = bottom + n
30+
nrects = len(left)
31+
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)
54+
ax.add_patch(patch)
55+
56+
ax.set_xlim(left[0], right[-1])
57+
ax.set_ylim(bottom.min(), top.max())
58+
59+
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.