python三维生命游戏(二)-------三维生命游戏(不完全体+3d建模)

import numpy as np

chushi = 6

a=np.zeros((chushi,chushi,chushi))#建立三维矩阵
b[0]
for i in range(0,chushi):
    
    b1=np.random.randint(2, size=(chushi, chushi))#二维矩阵的随机数
    a[i]=b1
    #b3=np.sum(b1,axis=0)
    #b2=np.sum(b1,axis=1)
    print(a)

3D图形在数据分析、数据建模、图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何使用python进行3D图形的绘制,包括3D散点、3D表面、3D轮廓、3D直线(曲线)以及3D文字等的绘制。


准备工作:

python中绘制3D图形,依旧使用常用的绘图模块matplotlib,但需要安装mpl_toolkits工具包,安装方法如下:windows命令行进入到python安装目录下的Scripts文件夹下,执行: pip install --upgrade matplotlib即可;linux环境下直接执行该命令。

安装好这个模块后,即可调用mpl_tookits下的mplot3d类进行3D图形的绘制。

下面以实例进行说明。


1、3D表面形状的绘制

[python]  view plain  copy
  1. from mpl_toolkits.mplot3d import Axes3D  
  2. import matplotlib.pyplot as plt  
  3. import numpy as np  
  4.   
  5. fig = plt.figure()  
  6. ax = fig.add_subplot(111, projection='3d')  
  7.   
  8. # Make data  
  9. u = np.linspace(02 * np.pi, 100)  
  10. v = np.linspace(0, np.pi, 100)  
  11. x = 10 * np.outer(np.cos(u), np.sin(v))  
  12. y = 10 * np.outer(np.sin(u), np.sin(v))  
  13. z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))  
  14.   
  15. # Plot the surface  
  16. ax.plot_surface(x, y, z, color='b')  
  17.   
  18. plt.show()  

这段代码是绘制一个3D的椭球表面,结果如下:



2、3D直线(曲线)的绘制

[python]  view plain  copy
  1. import matplotlib as mpl  
  2. from mpl_toolkits.mplot3d import Axes3D  
  3. import numpy as np  
  4. import matplotlib.pyplot as plt  
  5.   
  6. mpl.rcParams['legend.fontsize'] = 10  
  7.   
  8. fig = plt.figure()  
  9. ax = fig.gca(projection='3d')  
  10. theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)  
  11. z = np.linspace(-22100)  
  12. r = z**2 + 1  
  13. x = r * np.sin(theta)  
  14. y = r * np.cos(theta)  
  15. ax.plot(x, y, z, label='parametric curve')  
  16. ax.legend()  
  17.   
  18. plt.show()  

这段代码用于绘制一个螺旋状3D曲线,结果如下:



3、绘制3D轮廓

[python]  view plain  copy
  1. from mpl_toolkits.mplot3d import axes3d  
  2. import matplotlib.pyplot as plt  
  3. from matplotlib import cm  
  4.   
  5. fig = plt.figure()  
  6. ax = fig.gca(projection='3d')  
  7. X, Y, Z = axes3d.get_test_data(0.05)  
  8. cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)  
  9. cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)  
  10. cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)  
  11.   
  12. ax.set_xlabel('X')  
  13. ax.set_xlim(-4040)  
  14. ax.set_ylabel('Y')  
  15. ax.set_ylim(-4040)  
  16. ax.set_zlabel('Z')  
  17. ax.set_zlim(-100100)  
  18.   
  19. plt.show()  

绘制结果如下:



4、绘制3D直方图

[python]  view plain  copy
  1. from mpl_toolkits.mplot3d import Axes3D  
  2. import matplotlib.pyplot as plt  
  3. import numpy as np  
  4.   
  5. fig = plt.figure()  
  6. ax = fig.add_subplot(111, projection='3d')  
  7. x, y = np.random.rand(2100) * 4  
  8. hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[04], [04]])  
  9.   
  10. # Construct arrays for the anchor positions of the 16 bars.  
  11. # Note: np.meshgrid gives arrays in (ny, nx) so we use 'F' to flatten xpos,  
  12. # ypos in column-major order. For numpy >= 1.7, we could instead call meshgrid  
  13. # with indexing='ij'.  
  14. xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25)  
  15. xpos = xpos.flatten('F')  
  16. ypos = ypos.flatten('F')  
  17. zpos = np.zeros_like(xpos)  
  18.   
  19. # Construct arrays with the dimensions for the 16 bars.  
  20. dx = 0.5 * np.ones_like(zpos)  
  21. dy = dx.copy()  
  22. dz = hist.flatten()  
  23.   
  24. ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')  
  25.   
  26. plt.show()  

绘制结果如下:


5、绘制3D网状线

[python]  view plain  copy
  1. from mpl_toolkits.mplot3d import axes3d  
  2. import matplotlib.pyplot as plt  
  3.   
  4.   
  5. fig = plt.figure()  
  6. ax = fig.add_subplot(111, projection='3d')  
  7.   
  8. # Grab some test data.  
  9. X, Y, Z = axes3d.get_test_data(0.05)  
  10.   
  11. # Plot a basic wireframe.  
  12. ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)  
  13.   
  14. plt.show()  

绘制结果如下:



6、绘制3D三角面片图

[python]  view plain  copy
  1. from mpl_toolkits.mplot3d import Axes3D  
  2. import matplotlib.pyplot as plt  
  3. import numpy as np  
  4.   
  5.   
  6. n_radii = 8  
  7. n_angles = 36  
  8.   
  9. # Make radii and angles spaces (radius r=0 omitted to eliminate duplication).  
  10. radii = np.linspace(0.1251.0, n_radii)  
  11. angles = np.linspace(02*np.pi, n_angles, endpoint=False)  
  12.   
  13. # Repeat all angles for each radius.  
  14. angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)  
  15.   
  16. # Convert polar (radii, angles) coords to cartesian (x, y) coords.  
  17. # (0, 0) is manually added at this stage,  so there will be no duplicate  
  18. # points in the (x, y) plane.  
  19. x = np.append(0, (radii*np.cos(angles)).flatten())  
  20. y = np.append(0, (radii*np.sin(angles)).flatten())  
  21.   
  22. # Compute z to make the pringle surface.  
  23. z = np.sin(-x*y)  
  24.   
  25. fig = plt.figure()  
  26. ax = fig.gca(projection='3d')  
  27.   
  28. ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)  
  29.   
  30. plt.show()  

绘制结果如下:



7、绘制3D散点图

[python]  view plain  copy
  1. from mpl_toolkits.mplot3d import Axes3D  
  2. import matplotlib.pyplot as plt  
  3. import numpy as np  
  4.   
  5.   
  6. def randrange(n, vmin, vmax):  
  7.     ''''' 
  8.     Helper function to make an array of random numbers having shape (n, ) 
  9.     with each number distributed Uniform(vmin, vmax). 
  10.     '''  
  11.     return (vmax - vmin)*np.random.rand(n) + vmin  
  12.   
  13. fig = plt.figure()  
  14. ax = fig.add_subplot(111, projection='3d')  
  15.   
  16. n = 100  
  17.   
  18. # For each set of style and range settings, plot n random points in the box  
  19. # defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].  
  20. for c, m, zlow, zhigh in [('r''o', -50, -25), ('b''^', -30, -5)]:  
  21.     xs = randrange(n, 2332)  
  22.     ys = randrange(n, 0100)  
  23.     zs = randrange(n, zlow, zhigh)  
  24.     ax.scatter(xs, ys, zs, c=c, marker=m)  
  25.   
  26. ax.set_xlabel('X Label')  
  27. ax.set_ylabel('Y Label')  
  28. ax.set_zlabel('Z Label')  
  29.   
  30. plt.show()  

绘制结果如下:



8、绘制3D文字

[python]  view plain  copy
  1. from mpl_toolkits.mplot3d import Axes3D  
  2. import matplotlib.pyplot as plt  
  3.   
  4.   
  5. fig = plt.figure()  
  6. ax = fig.gca(projection='3d')  
  7.   
  8. # Demo 1: zdir  
  9. zdirs = (None'x''y''z', (110), (111))  
  10. xs = (144941)  
  11. ys = (2581012)  
  12. zs = (1038918)  
  13.   
  14. for zdir, x, y, z in zip(zdirs, xs, ys, zs):  
  15.     label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)  
  16.     ax.text(x, y, z, label, zdir)  
  17.   
  18. # Demo 2: color  
  19. ax.text(900"red", color='red')  
  20.   
  21. # Demo 3: text2D  
  22. # Placement 0, 0 would be the bottom left, 1, 1 would be the top right.  
  23. ax.text2D(0.050.95"2D Text", transform=ax.transAxes)  
  24.   
  25. # Tweaking display region and labels  
  26. ax.set_xlim(010)  
  27. ax.set_ylim(010)  
  28. ax.set_zlim(010)  
  29. ax.set_xlabel('X axis')  
  30. ax.set_ylabel('Y axis')  
  31. ax.set_zlabel('Z axis')  
  32.   
  33. plt.show()  

绘制结果如下:



9、3D条状图

[python]  view plain  copy
  1. from mpl_toolkits.mplot3d import Axes3D  
  2. import matplotlib.pyplot as plt  
  3. import numpy as np  
  4.   
  5. fig = plt.figure()  
  6. ax = fig.add_subplot(111, projection='3d')  
  7. for c, z in zip(['r''g''b''y'], [3020100]):  
  8.     xs = np.arange(20)  
  9.     ys = np.random.rand(20)  
  10.   
  11.     # You can provide either a single color or an array. To demonstrate this,  
  12.     # the first bar of each set will be colored cyan.  
  13.     cs = [c] * len(xs)  
  14.     cs[0] = 'c'  
  15.     ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)  
  16.   
  17. ax.set_xlabel('X')  
  18. ax.set_ylabel('Y')  
  19. ax.set_zlabel('Z')  
  20.   
  21. plt.show()  

绘制结果如下:



猜你喜欢

转载自blog.csdn.net/qq_36142114/article/details/80269166