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

Skip to content

Commit 6787135

Browse files
committed
mplot3d: add examples, fix NaN bug
svn path=/trunk/matplotlib/; revision=7154
1 parent 7b846ea commit 6787135

8 files changed

Lines changed: 112 additions & 2 deletions

File tree

examples/mplot3d/contour.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from mpl_toolkits.mplot3d import axes3d
2+
import pylab
3+
import random
4+
5+
fig = pylab.figure()
6+
ax = axes3d.Axes3D(fig)
7+
X, Y, Z = axes3d.get_test_data(0.05)
8+
cset = ax.contour3D(X, Y, Z)
9+
ax.clabel(cset, fontsize=9, inline=1)
10+
11+
pylab.show()
12+

examples/mplot3d/contourf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from mpl_toolkits.mplot3d import axes3d
2+
import pylab
3+
import random
4+
5+
fig = pylab.figure()
6+
ax = axes3d.Axes3D(fig)
7+
X, Y, Z = axes3d.get_test_data(0.05)
8+
cset = ax.contourf3D(X, Y, Z)
9+
ax.clabel(cset, fontsize=9, inline=1)
10+
11+
pylab.show()
12+

examples/mplot3d/polys.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from mpl_toolkits.mplot3d import Axes3D
2+
from matplotlib.collections import PolyCollection
3+
from matplotlib.colors import colorConverter
4+
import pylab
5+
import random
6+
import numpy as np
7+
8+
fig = pylab.figure()
9+
ax = Axes3D(fig)
10+
11+
cc = lambda arg: colorConverter.to_rgba(arg, alpha=0.6)
12+
13+
xs = np.arange(0, 10, 0.4)
14+
verts = []
15+
zs = [0.0, 1.0, 2.0, 3.0]
16+
for z in zs:
17+
ys = [random.random() for x in xs]
18+
ys[0], ys[-1] = 0, 0
19+
verts.append(zip(xs, ys))
20+
21+
poly = PolyCollection(verts, facecolors = [cc('r'), cc('g'), cc('b'),
22+
cc('y')])
23+
poly.set_alpha(0.7)
24+
ax.add_collection(poly, zs=zs, dir='y')
25+
26+
ax.set_xlim(0, 10)
27+
ax.set_ylim(-1, 4)
28+
ax.set_zlim(0, 1)
29+
30+
pylab.show()
31+

examples/mplot3d/scatter.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from mpl_toolkits.mplot3d import Axes3D
2+
import pylab
3+
import random
4+
5+
fig = pylab.figure()
6+
ax = Axes3D(fig)
7+
n = 100
8+
for c, zl, zh in [('r', -50, -25), ('b', -30, -5)]:
9+
xs, ys, zs = zip(*
10+
[(random.randrange(23, 32),
11+
random.randrange(100),
12+
random.randrange(zl, zh)
13+
) for i in range(n)])
14+
ax.scatter3D(xs, ys, zs, c=c)
15+
16+
ax.set_xlabel('X Label')
17+
ax.set_ylabel('Y Label')
18+
ax.set_zlabel('Z Label')
19+
20+
pylab.show()
21+

examples/mplot3d/surface.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from mpl_toolkits.mplot3d import Axes3D
2+
import pylab
3+
import random
4+
import numpy as np
5+
6+
fig = pylab.figure()
7+
ax = Axes3D(fig)
8+
X = np.arange(-5, 5, 0.5)
9+
Y = np.arange(-5, 5, 0.5)
10+
X, Y = np.meshgrid(X, Y)
11+
R = np.sqrt(X**2 + Y**2)
12+
Z = np.sin(R)
13+
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, color='forestgreen')
14+
15+
pylab.show()
16+

examples/mplot3d/wire.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from mpl_toolkits.mplot3d import axes3d
2+
import pylab
3+
import random
4+
import numpy as np
5+
6+
fig = pylab.figure()
7+
ax = axes3d.Axes3D(fig)
8+
X, Y, Z = axes3d.get_test_data(0.05)
9+
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
10+
11+
pylab.show()
12+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from axes3d import Axes3D

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ def plot_surface(self, X, Y, Z, *args, **kwargs):
539539
rstride = kwargs.pop('rstride', 10)
540540
cstride = kwargs.pop('cstride', 10)
541541

542+
color = kwargs.pop('color', 'b')
543+
color = np.array(colorConverter.to_rgba(color))
544+
542545
polys = []
543546
boxes = []
544547
for rs in np.arange(0,rows-1,rstride):
@@ -567,8 +570,10 @@ def plot_surface(self, X, Y, Z, *args, **kwargs):
567570
shade.append(np.dot(n,[-1,-1,0.5]))
568571
lines.append((box[0],n+box[0]))
569572

570-
color = np.array([0,0,1,1])
571-
norm = Normalize(min(shade),max(shade))
573+
shade = np.array(shade)
574+
mask = ~np.isnan(shade)
575+
norm = Normalize(min(shade[mask]), max(shade[mask]))
576+
572577
colors = [color * (0.5+norm(v)*0.5) for v in shade]
573578
for c in colors: c[3] = 1
574579
polyc = art3d.Poly3DCollection(polys, facecolors=colors, *args, **kwargs)

0 commit comments

Comments
 (0)