Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commita869cc0

Browse files
committed
Merge pull request#6690 from TrishGillett/mplot3d-examples-MEP12-b
Tidying up and tweaking mplot3d examples [MEP12]Conflicts:examples/mplot3d/2dcollections3d_demo.pyConflict resolved by taking the version from#6690
1 parent421327d commita869cc0

17 files changed

+343
-101
lines changed
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
1+
'''
2+
Demonstrates using ax.plot's zdir keyword to plot 2D scatterplot data on
3+
selective axes of a 3D plot.
4+
'''
5+
16
frommpl_toolkits.mplot3dimportAxes3D
27
importnumpyasnp
38
importmatplotlib.pyplotasplt
49

510
fig=plt.figure()
611
ax=fig.gca(projection='3d')
712

13+
# Plot a sin curve using the x and y axes.
814
x=np.linspace(0,1,100)
915
y=np.sin(x*2*np.pi)/2+0.5
10-
ax.plot(x,y,zs=0,zdir='z',label='zs=0, zdir=z')
16+
ax.plot(x,y,zs=0,zdir='z',label='curve in (x,y)')
1117

18+
# Plot scatterplot data (20 2D points per colour) on the x and z axes.
1219
colors= ('r','g','b','k')
20+
x=np.random.sample(20*len(colors))
21+
y=np.random.sample(20*len(colors))
22+
c_list= []
1323
forcincolors:
14-
x=np.random.sample(20)
15-
y=np.random.sample(20)
16-
ax.scatter(x,y,0,zdir='y',c=c)
24+
c_list.append([c]*20)
25+
# By using zdir='y', the y value of these points is fixed to the zs value 0
26+
# and the (x,y) points are plotted on the x and z axes.
27+
ax.scatter(x,y,zs=0,zdir='y',c=c_list,label='points in (x,z)')
1728

29+
# Make legend, set axes limits and labels
1830
ax.legend()
19-
ax.set_xlim3d(0,1)
20-
ax.set_ylim3d(0,1)
21-
ax.set_zlim3d(0,1)
31+
ax.set_xlim(0,1)
32+
ax.set_ylim(0,1)
33+
ax.set_zlim(0,1)
34+
ax.set_xlabel('X')
35+
ax.set_ylabel('Y')
36+
ax.set_zlabel('Z')
37+
38+
# Customize the view angle so it's easier to see that the scatter points lie
39+
# on the plane y=0
40+
ax.view_init(elev=20.,azim=-35)
2241

2342
plt.show()

‎examples/mplot3d/quiver3d_demo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
'''
2+
==============
3+
3D quiver plot
4+
==============
5+
6+
Demonstrates plotting directional arrows at points on a 3d meshgrid.
7+
'''
8+
19
frommpl_toolkits.mplot3dimportaxes3d
210
importmatplotlib.pyplotasplt
311
importnumpyasnp
412

513
fig=plt.figure()
614
ax=fig.gca(projection='3d')
715

16+
# Make the grid
817
x,y,z=np.meshgrid(np.arange(-0.8,1,0.2),
918
np.arange(-0.8,1,0.2),
1019
np.arange(-0.8,1,0.8))
1120

21+
# Make the direction data for the arrows
1222
u=np.sin(np.pi*x)*np.cos(np.pi*y)*np.cos(np.pi*z)
1323
v=-np.cos(np.pi*x)*np.sin(np.pi*y)*np.cos(np.pi*z)
1424
w= (np.sqrt(2.0/3.0)*np.cos(np.pi*x)*np.cos(np.pi*y)*
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
'''
2+
==================
3+
Rotating a 3D plot
4+
==================
5+
6+
A very simple animation of a rotating 3D plot.
7+
8+
See wire3d_animation_demo for another simple example of animating a 3D plot.
9+
'''
10+
111
frommpl_toolkits.mplot3dimportaxes3d
212
importmatplotlib.pyplotasplt
3-
importnumpyasnp
4-
513

614
fig=plt.figure()
715
ax=fig.add_subplot(111,projection='3d')
16+
17+
# load some test data for demonstration and plot a wireframe
818
X,Y,Z=axes3d.get_test_data(0.1)
919
ax.plot_wireframe(X,Y,Z,rstride=5,cstride=5)
1020

21+
# rotate the axes and update
1122
forangleinrange(0,360):
1223
ax.view_init(30,angle)
1324
plt.draw()
25+
plt.pause(.001)

‎examples/mplot3d/scatter3d_demo.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
1-
importnumpyasnp
1+
'''
2+
==============
3+
3D scatterplot
4+
==============
5+
6+
Demonstration of a basic scatterplot in 3D.
7+
'''
8+
29
frommpl_toolkits.mplot3dimportAxes3D
310
importmatplotlib.pyplotasplt
11+
importnumpyasnp
412

513

614
defrandrange(n,vmin,vmax):
15+
'''
16+
Helper function to make an array of random numbers having shape (n, )
17+
with each number distributed Uniform(vmin, vmax).
18+
'''
719
return (vmax-vmin)*np.random.rand(n)+vmin
820

921
fig=plt.figure()
1022
ax=fig.add_subplot(111,projection='3d')
23+
1124
n=100
12-
forc,m,zl,zhin [('r','o',-50,-25), ('b','^',-30,-5)]:
25+
26+
# For each set of style and range settings, plot n random points in the box
27+
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
28+
forc,m,zlow,zhighin [('r','o',-50,-25), ('b','^',-30,-5)]:
1329
xs=randrange(n,23,32)
1430
ys=randrange(n,0,100)
15-
zs=randrange(n,zl,zh)
31+
zs=randrange(n,zlow,zhigh)
1632
ax.scatter(xs,ys,zs,c=c,marker=m)
1733

1834
ax.set_xlabel('X Label')

‎examples/mplot3d/subplot3d_demo.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
1-
frommpl_toolkits.mplot3d.axes3dimportAxes3D
2-
importmatplotlib.pyplotasplt
1+
'''
2+
====================
3+
3D plots as subplots
4+
====================
35
6+
Demonstrate including 3D plots as subplots.
7+
'''
48

5-
# imports specific to the plots in this example
6-
importnumpyasnp
9+
importmatplotlib.pyplotasplt
10+
frommpl_toolkits.mplot3d.axes3dimportAxes3D,get_test_data
711
frommatplotlibimportcm
8-
frommpl_toolkits.mplot3d.axes3dimportget_test_data
12+
importnumpyasnp
913

10-
# Twice as wide as it is tall.
14+
15+
# set up a figure twice as wide as it is tall
1116
fig=plt.figure(figsize=plt.figaspect(0.5))
1217

13-
#---- First subplot
18+
#===============
19+
# First subplot
20+
#===============
21+
# set up the axes for the first plot
1422
ax=fig.add_subplot(1,2,1,projection='3d')
23+
24+
# plot a 3D surface like in the example mplot3d/surface3d_demo
1525
X=np.arange(-5,5,0.25)
1626
Y=np.arange(-5,5,0.25)
1727
X,Y=np.meshgrid(X,Y)
1828
R=np.sqrt(X**2+Y**2)
1929
Z=np.sin(R)
2030
surf=ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=cm.coolwarm,
2131
linewidth=0,antialiased=False)
22-
ax.set_zlim3d(-1.01,1.01)
23-
32+
ax.set_zlim(-1.01,1.01)
2433
fig.colorbar(surf,shrink=0.5,aspect=10)
2534

26-
#---- Second subplot
35+
#===============
36+
# Second subplot
37+
#===============
38+
# set up the axes for the second plot
2739
ax=fig.add_subplot(1,2,2,projection='3d')
40+
41+
# plot a 3D wireframe like in the example mplot3d/wire3d_demo
2842
X,Y,Z=get_test_data(0.05)
2943
ax.plot_wireframe(X,Y,Z,rstride=10,cstride=10)
3044

‎examples/mplot3d/surface3d_demo.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
1+
'''
2+
======================
3+
3D surface (color map)
4+
======================
5+
6+
Demonstrates plotting a 3D surface colored with the coolwarm color map.
7+
The surface is made opaque by using antialiased=False.
8+
9+
Also demonstrates using the LinearLocator and custom formatting for the
10+
z axis tick labels.
11+
'''
12+
113
frommpl_toolkits.mplot3dimportAxes3D
14+
importmatplotlib.pyplotasplt
215
frommatplotlibimportcm
316
frommatplotlib.tickerimportLinearLocator,FormatStrFormatter
4-
importmatplotlib.pyplotasplt
517
importnumpyasnp
618

19+
720
fig=plt.figure()
821
ax=fig.gca(projection='3d')
22+
23+
# Make data.
924
X=np.arange(-5,5,0.25)
1025
Y=np.arange(-5,5,0.25)
1126
X,Y=np.meshgrid(X,Y)
1227
R=np.sqrt(X**2+Y**2)
1328
Z=np.sin(R)
29+
30+
# Plot the surface.
1431
surf=ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=cm.coolwarm,
1532
linewidth=0,antialiased=False)
16-
ax.set_zlim(-1.01,1.01)
1733

34+
# Customize the z axis.
35+
ax.set_zlim(-1.01,1.01)
1836
ax.zaxis.set_major_locator(LinearLocator(10))
1937
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
2038

39+
# Add a color bar which maps values to colors.
2140
fig.colorbar(surf,shrink=0.5,aspect=5)
2241

2342
plt.show()

‎examples/mplot3d/surface3d_demo2.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1+
'''
2+
========================
3+
3D surface (solid color)
4+
========================
5+
6+
Demonstrates a very basic plot of a 3D surface using a solid color.
7+
'''
8+
19
frommpl_toolkits.mplot3dimportAxes3D
210
importmatplotlib.pyplotasplt
311
importnumpyasnp
412

13+
514
fig=plt.figure()
615
ax=fig.add_subplot(111,projection='3d')
716

17+
# Make data
818
u=np.linspace(0,2*np.pi,100)
919
v=np.linspace(0,np.pi,100)
10-
1120
x=10*np.outer(np.cos(u),np.sin(v))
1221
y=10*np.outer(np.sin(u),np.sin(v))
1322
z=10*np.outer(np.ones(np.size(u)),np.cos(v))
23+
24+
# Plot the surface
1425
ax.plot_surface(x,y,z,rstride=4,cstride=4,color='b')
1526

1627
plt.show()

‎examples/mplot3d/surface3d_demo3.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
'''
2+
=========================
3+
3D surface (checkerboard)
4+
=========================
5+
6+
Demonstrates plotting a 3D surface colored in a checkerboard pattern.
7+
'''
8+
19
frommpl_toolkits.mplot3dimportAxes3D
10+
importmatplotlib.pyplotasplt
211
frommatplotlibimportcm
312
frommatplotlib.tickerimportLinearLocator
4-
importmatplotlib.pyplotasplt
513
importnumpyasnp
614

15+
716
fig=plt.figure()
817
ax=fig.gca(projection='3d')
18+
19+
# Make data.
920
X=np.arange(-5,5,0.25)
1021
xlen=len(X)
1122
Y=np.arange(-5,5,0.25)
@@ -14,16 +25,20 @@
1425
R=np.sqrt(X**2+Y**2)
1526
Z=np.sin(R)
1627

28+
# Create an empty array of strings with the same shape as the meshgrid, and
29+
# populate it with two colors in a checkerboard pattern.
1730
colortuple= ('y','b')
1831
colors=np.empty(X.shape,dtype=str)
1932
foryinrange(ylen):
2033
forxinrange(xlen):
2134
colors[x,y]=colortuple[(x+y)%len(colortuple)]
2235

36+
# Plot the surface with face colors taken from the array we made.
2337
surf=ax.plot_surface(X,Y,Z,rstride=1,cstride=1,facecolors=colors,
2438
linewidth=0)
2539

26-
ax.set_zlim3d(-1,1)
40+
# Customize the z axis.
41+
ax.set_zlim(-1,1)
2742
ax.w_zaxis.set_major_locator(LinearLocator(6))
2843

2944
plt.show()
Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
1-
# By Armin Moser
1+
'''
2+
=================================
3+
3D surface with polar coordinates
4+
=================================
5+
6+
Demonstrates plotting a surface defined in polar coordinates.
7+
Uses the reversed version of the YlGnBu color map.
8+
Also demonstrates writing axis labels with latex math mode.
9+
10+
Example contributed by Armin Moser.
11+
'''
212

313
frommpl_toolkits.mplot3dimportAxes3D
4-
importmatplotlib
5-
importnumpyasnp
6-
frommatplotlibimportcm
714
frommatplotlibimportpyplotasplt
8-
step=0.04
9-
maxval=1.0
15+
importnumpyasnp
16+
17+
1018
fig=plt.figure()
1119
ax=fig.add_subplot(111,projection='3d')
1220

13-
#create supporting points in polar coordinates
21+
#Create the mesh in polar coordinates and compute corresponding Z.
1422
r=np.linspace(0,1.25,50)
1523
p=np.linspace(0,2*np.pi,50)
1624
R,P=np.meshgrid(r,p)
17-
# transform them to cartesian system
25+
Z= ((R**2-1)**2)
26+
27+
# Express the mesh in the cartesian system.
1828
X,Y=R*np.cos(P),R*np.sin(P)
1929

20-
Z= ((R**2-1)**2)
21-
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=cm.YlGnBu_r)
22-
ax.set_zlim3d(0,1)
30+
# Plot the surface.
31+
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.cm.YlGnBu_r)
32+
33+
# Tweak the limits and add latex math labels.
34+
ax.set_zlim(0,1)
2335
ax.set_xlabel(r'$\phi_\mathrm{real}$')
2436
ax.set_ylabel(r'$\phi_\mathrm{im}$')
2537
ax.set_zlabel(r'$V(\phi)$')
38+
2639
plt.show()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp