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

Skip to content

Commit c6156ac

Browse files
committed
Merged revisions 7428-7433 via svnmerge from
https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v0_99_maint ........ r7428 | jdh2358 | 2009-08-08 02:21:29 -1000 (Sat, 08 Aug 2009) | 1 line clean up mplot3d examples: use pyplot noy pylab and numpy rather than list comps and python random module ........ r7429 | jdh2358 | 2009-08-08 03:53:24 -1000 (Sat, 08 Aug 2009) | 1 line two new examples using a compund path for a histogram; one animated ........ r7430 | jdh2358 | 2009-08-08 03:58:52 -1000 (Sat, 08 Aug 2009) | 1 line two new examples using a compund path for a histogram; one animated ........ r7431 | jdh2358 | 2009-08-08 05:10:08 -1000 (Sat, 08 Aug 2009) | 1 line use a class helper method to make the compound path from polys ........ r7432 | jdh2358 | 2009-08-08 05:16:57 -1000 (Sat, 08 Aug 2009) | 1 line simplify poly array in example ........ r7433 | efiring | 2009-08-08 08:16:01 -1000 (Sat, 08 Aug 2009) | 2 lines Fix excessive line length in annotations.rst ........ svn path=/trunk/matplotlib/; revision=7434
2 parents aa82808 + 52f7369 commit c6156ac

17 files changed

Lines changed: 212 additions & 75 deletions

doc/users/annotations.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,18 @@ from the text to the annotated point by giving a dictionary of arrow
5656
properties in the optional keyword argument ``arrowprops``.
5757

5858

59-
==================== ===========================================================================
59+
==================== =====================================================
6060
``arrowprops`` key description
61-
==================== ===========================================================================
61+
==================== =====================================================
6262
width the width of the arrow in points
6363
frac the fraction of the arrow length occupied by the head
6464
headwidth the width of the base of the arrow head in points
65-
shrink move the tip and base some percent away from the annotated point and text
66-
\*\*kwargs any key for :class:`matplotlib.patches.Polygon`, eg ``facecolor``
67-
==================== ===========================================================================
65+
shrink move the tip and base some percent away from
66+
the annotated point and text
67+
68+
\*\*kwargs any key for :class:`matplotlib.patches.Polygon`,
69+
e.g. ``facecolor``
70+
==================== =====================================================
6871

6972

7073
In the example below, the ``xy`` point is in native coordinates
@@ -78,4 +81,7 @@ keyword args like ``horizontalalignment``, ``verticalalignment`` and
7881
.. plot:: pyplots/annotation_polar.py
7982
:include-source:
8083

81-
For more on all the wild and wonderful things you can do with annotations, including fancy arrows, see :ref:`plotting-guide-annotation` and :ref:`pylab_examples-annotation_demo`.
84+
For more on all the wild and wonderful things you can do with
85+
annotations, including fancy arrows, see :ref:`plotting-guide-annotation`
86+
and :ref:`pylab_examples-annotation_demo`.
87+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
This example shows how to use a path patch to draw a bunch of
3+
rectangles for an animated histogram
4+
"""
5+
import time
6+
import numpy as np
7+
import matplotlib
8+
matplotlib.use('TkAgg') # do this before importing pylab
9+
10+
import matplotlib.pyplot as plt
11+
import matplotlib.patches as patches
12+
import matplotlib.path as path
13+
14+
fig = plt.figure()
15+
ax = fig.add_subplot(111)
16+
17+
# histogram our data with numpy
18+
data = np.random.randn(1000)
19+
n, bins = np.histogram(data, 100)
20+
21+
# get the corners of the rectangles for the histogram
22+
left = np.array(bins[:-1])
23+
right = np.array(bins[1:])
24+
bottom = np.zeros(len(left))
25+
top = bottom + n
26+
nrects = len(left)
27+
28+
# here comes the tricky part -- we have to set up the vertex and path
29+
# codes arrays using moveto, lineto and closepoly
30+
31+
# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
32+
# CLOSEPOLY; the vert for the closepoly is ignored but we still need
33+
# it to keep the codes aligned with the vertices
34+
nverts = nrects*(1+3+1)
35+
verts = np.zeros((nverts, 2))
36+
codes = np.ones(nverts, int) * path.Path.LINETO
37+
codes[0::5] = path.Path.MOVETO
38+
codes[4::5] = path.Path.CLOSEPOLY
39+
verts[0::5,0] = left
40+
verts[0::5,1] = bottom
41+
verts[1::5,0] = left
42+
verts[1::5,1] = top
43+
verts[2::5,0] = right
44+
verts[2::5,1] = top
45+
verts[3::5,0] = right
46+
verts[3::5,1] = bottom
47+
48+
barpath = path.Path(verts, codes)
49+
patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
50+
ax.add_patch(patch)
51+
52+
ax.set_xlim(left[0], right[-1])
53+
ax.set_ylim(bottom.min(), top.max())
54+
55+
def animate():
56+
if animate.cnt>=100:
57+
return
58+
59+
animate.cnt += 1
60+
# simulate new data coming in
61+
data = np.random.randn(1000)
62+
n, bins = np.histogram(data, 100)
63+
top = bottom + n
64+
verts[1::5,1] = top
65+
verts[2::5,1] = top
66+
fig.canvas.draw()
67+
fig.canvas.manager.window.after(100, animate)
68+
animate.cnt = 0
69+
fig.canvas.manager.window.after(100, animate)
70+
plt.show()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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, 50)
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+
31+
32+
# we need a (numrects x numsides x 2) numpy array for the path helper
33+
# function to build a compound path
34+
XY = np.array([[left,left,right,right], [bottom,top,top,bottom]]).T
35+
36+
# get the Path object
37+
barpath = path.Path.make_compound_path_from_polys(XY)
38+
39+
# make a patch out of it
40+
patch = patches.PathPatch(barpath, facecolor='blue', edgecolor='gray', alpha=0.8)
41+
ax.add_patch(patch)
42+
43+
# update the view limits
44+
ax.set_xlim(left[0], right[-1])
45+
ax.set_ylim(bottom.min(), top.max())
46+
47+
plt.show()

examples/mplot3d/2dcollections3d_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from mpl_toolkits.mplot3d import Axes3D
22
import numpy as np
3-
import pylab
3+
import matplotlib.pyplot as plt
44

5-
fig = pylab.figure()
5+
fig = plt.figure()
66
ax = Axes3D(fig)
77

88
x = np.linspace(0, 1, 100)
@@ -20,5 +20,5 @@
2020
ax.set_ylim3d(0, 1)
2121
ax.set_zlim3d(0, 1)
2222

23-
pylab.show()
23+
plt.show()
2424

examples/mplot3d/bars3d_demo.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
from mpl_toolkits.mplot3d import Axes3D
2-
from matplotlib.collections import PolyCollection
3-
from matplotlib.colors import colorConverter
4-
import pylab
5-
import random
2+
import matplotlib.pyplot as plt
63
import numpy as np
74

8-
fig = pylab.figure()
5+
fig = plt.figure()
96
ax = Axes3D(fig)
107
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
118
xs = np.arange(20)
12-
ys = [random.random() for x in xs]
9+
ys = np.random.rand(20)
1310
ax.bar(xs, ys, zs=z, zdir='y', color=c, alpha=0.8)
1411

1512
ax.set_xlabel('X')
1613
ax.set_ylabel('Y')
1714
ax.set_zlabel('Z')
1815

19-
pylab.show()
16+
plt.show()
2017

examples/mplot3d/contour3d_demo.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from mpl_toolkits.mplot3d import axes3d
2-
import pylab
3-
import random
2+
import matplotlib.pyplot as plt
43

5-
fig = pylab.figure()
4+
fig = plt.figure()
65
ax = axes3d.Axes3D(fig)
76
X, Y, Z = axes3d.get_test_data(0.05)
87
cset = ax.contour(X, Y, Z)
98
ax.clabel(cset, fontsize=9, inline=1)
109

11-
pylab.show()
10+
plt.show()
1211

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from mpl_toolkits.mplot3d import axes3d
2-
import pylab
3-
import random
2+
import matplotlib.pyplot as plt
43

5-
fig = pylab.figure()
4+
fig = plt.figure()
65
ax = axes3d.Axes3D(fig)
76
X, Y, Z = axes3d.get_test_data(0.05)
87
cset = ax.contour(X, Y, Z, 16, extend3d=True)
98
ax.clabel(cset, fontsize=9, inline=1)
109

11-
pylab.show()
10+
plt.show()
1211

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from mpl_toolkits.mplot3d import axes3d
2-
import pylab
3-
import random
2+
import matplotlib.pyplot as plt
43

5-
fig = pylab.figure()
4+
fig = plt.figure()
65
ax = axes3d.Axes3D(fig)
76
X, Y, Z = axes3d.get_test_data(0.05)
87
cset = ax.contourf(X, Y, Z)
98
ax.clabel(cset, fontsize=9, inline=1)
109

11-
pylab.show()
10+
plt.show()
1211

examples/mplot3d/hist3d_demo.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
from mpl_toolkits.mplot3d import Axes3D
2-
from matplotlib.collections import PolyCollection
3-
from matplotlib.colors import colorConverter
4-
import pylab
5-
import random
2+
import matplotlib.pyplot as plt
63
import numpy as np
74

8-
fig = pylab.figure()
5+
fig = plt.figure()
96
ax = Axes3D(fig)
10-
x = np.random.rand(100) * 4
11-
y = np.random.rand(100) * 4
7+
x, y = np.random.rand(2, 100) * 4
128
hist, xedges, yedges = np.histogram2d(x, y, bins=4)
139

1410
elements = (len(xedges) - 1) * (len(yedges) - 1)
15-
xpos, ypos = np.meshgrid(
16-
[xedges[i] + 0.25 for i in range(len(xedges) - 1)],
17-
[yedges[i] + 0.25 for i in range(len(yedges) - 1)])
11+
xpos, ypos = np.meshgrid(xedges[:-1]+0.25, yedges[:-1]+0.25)
12+
1813
xpos = xpos.flatten()
1914
ypos = ypos.flatten()
20-
zpos = [0] * elements
21-
dx = [0.5] * elements
22-
dy = [0.5] * elements
15+
zpos = np.zeros(elements)
16+
dx = 0.5 * np.ones_like(zpos)
17+
dy = dx.copy()
2318
dz = hist.flatten()
2419
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b')
2520

26-
pylab.show()
21+
plt.show()
2722

examples/mplot3d/lines3d_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import matplotlib as mpl
22
from mpl_toolkits.mplot3d import Axes3D
33
import numpy as np
4-
import pylab
4+
import matplotlib.pyplot as plt
55

66
mpl.rcParams['legend.fontsize'] = 10
77

8-
fig = pylab.figure()
8+
fig = plt.figure()
99
ax = Axes3D(fig)
1010
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
1111
z = np.linspace(-2, 2, 100)
@@ -15,5 +15,5 @@
1515
ax.plot(x, y, z, label='parametric curve')
1616
ax.legend()
1717

18-
pylab.show()
18+
plt.show()
1919

0 commit comments

Comments
 (0)