当前位置:实例文章 » Python实例» [文章]Python 3D可视化(一)

Python 3D可视化(一)

发布人:shili8 发布时间:2023-04-26 09:47 阅读次数:23

Python已经成为了一种广泛应用的编程语言,在数据可视化方面也有很多优秀的库和工具。而其中3D可视化是比较新颖且越来越受欢迎的技术。在本篇文章中,我们将介绍如何安装环境,并提供一些常见的Python 3D实例集锦。 二、简介 Python 3D可视化是使用Python编写的三维图形库。它可以显示各种类型的3D对象,如球体、立方体、箭头、线、平面等。此外,它还支持各种格式的导入和导出,例如OBJ、PLY、STL等。 三、环境安装 要使用Python进行3D可视化,需要安装一个3D图形库。这里我们推荐使用Mayavi库,它是一个功能丰富的3D科学数据可视化包。 要安装Mayavi,请先安装Anaconda或miniconda。然后在终端中运行以下命令: ``` conda install mayavi ``` 安装完成后,就可以使用以下命令导入Mayavi: ``` from mayavi import mlab ``` 四、python 3D实例集锦 以下是一些常见的Python 3D实例集锦,让你了解如何创建3D图形。 1、圆柱体 ``` import numpy as np import matplotlib.pyplot as plt from mayavi import mlab x, y = np.mgrid[-1:1:21j,-1:1:21j] z = x**2 + y**2 mlab.mesh(x, y, z) mlab.show() ``` 2、箭头 ``` import numpy as np from mayavi import mlab r = np.linspace(-2, 2, 100) x, y, z = np.meshgrid(r,r,r) u = np.sin(np.pi*x)*np.cos(np.pi*y)*np.cos(np.pi*z) v = -np.cos(np.pi*x)*np.sin(np.pi*y)*np.cos(np.pi*z) w = (np.sqrt(2.0/3.0)*np.cos(np.pi*x)*np.cos(np.pi*y)*np.sin(np.pi*z)) mlab.quiver3d(x, y, z, u, v, w, line_width=2, scale_factor=1) mlab.show() ``` 3、球形 ``` import numpy as np import matplotlib.pyplot as plt from mayavi import mlab theta = np.linspace(0, 2*np.pi, 30) phi = np.linspace(0, np.pi, 30) x = np.outer(np.cos(theta), np.sin(phi)) y = np.outer(np.sin(theta), np.sin(phi)) z = np.outer(np.ones(len(theta)), np.cos(phi)) mlab.mesh(x, y, z) mlab.show() ``` 4、平面 ``` import numpy as np import matplotlib.pyplot as plt from mayavi import mlab x, y = np.mgrid[-1:1:21j,-1:1:21j] z = np.zeros_like(x) mlab.surf(x, y, z) mlab.show() ``` 5、线型 ``` import numpy as np import matplotlib.pyplot as plt from mayavi import mlab x = np.linspace(-np.pi, np.pi, 100) y = np.sin(x) mlab.plot3d(x, y, np.zeros_like(x)) mlab.show() ``` 6、立方体 ``` from mayavi import mlab mlab.test_contour3d() mlab.show() ``` 7、圆锥体 ``` import numpy as np from mayavi import mlab theta = np.linspace(0, 2*np.pi, 30) r = np.linspace(0, 1, 30) r, theta = np.meshgrid(r, theta) x = r*np.cos(theta) y = r*np.sin(theta) z = np.sqrt(x**2 + y**2) mlab.mesh(x, y, z) mlab.show() ``` 8、六边形 ``` import numpy as np from mayavi import mlab theta = np.linspace(0, 2*np.pi, 7) radius = 1 x = radius*np.cos(theta) y = radius*np.sin(theta) mlab.plot3d(x, y, np.zeros_like(x), tube_radius=None) mlab.show() ``` 9、空心六边形 ``` import numpy as np from mayavi import mlab theta = np.linspace(0, 2*np.pi, 7) radius = 1 x = radius*np.cos(theta) y = radius*np.sin(theta) mlab.plot3d(x, y, np.zeros_like(x), tube_radius=0.1) mlab.show() ``` 10、茶壶 ``` import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from mayavi import mlab fig = plt.figure() ax = fig.gca(projection='3d') url = "https://gist.githubusercontent.com/rougier/e156be8f53b20b873bbe450d177de4ed/raw/8c4ca4e9d09825a263140c8e8e75891083c07a03/teapot.obj" with urllib.request.urlopen(url) as f: lines = f.read().decode('utf-8').splitlines() vertex = [] face = [] for l in lines: if l.startswith('#'): continue t = l.split() if not t: continue if t[0] == 'v': vertex.append(list(map(float, t[1:4]))) elif t[0] == 'f': face.append([int(i.split('/')[0])-1 for i in t[1:]]) vertex = np.array(vertex) face = np.array(face) mlab.triangular_mesh(vertex[:,0], vertex[:,1], vertex[:,2], face, representation='wireframe') mlab.show() ``` 11、兔子 ``` import urllib.request import io import numpy as np from mayavi import mlab url = "https://www.cs.cmu.edu/~mbz/personal/graphics/Stanford_Bunny/" filename = "stanford_bunny.ply" with urllib.request.urlopen(url+filename) as f: data = f.read().decode('utf-8') # Remove the header data = '\n'.join(data.split('\n')[14:]) # The delimiter can be any character that is not alphanumeric x,y,z = np.loadtxt(io.StringIO(data), delimiter=';', usecols=(0,1,2), unpack=True) # 3D visualization with Mayavi mlab.points3d(x, y, z, mode='point') mlab.show() ``` 12、飞机 ``` import urllib.request import io import numpy as np from mayavi import mlab url = "https://www.cs.cmu.edu/~mbz/personal/graphics/Wright_Flyer/" filename = "wright_flyer.ply" with urllib.request.urlopen(url+filename) as f: data = f.read().decode('utf-8') # Remove the header data = '\n'.join(data.split('\n')[14:]) # The delimiter can be any character that is not alphanumeric x,y,z = np.loadtxt(io.StringIO(data), delimiter=';', usecols=(0,1,2), unpack=True) # 3D visualization with Mayavi mlab.points3d(x, y, z, mode='point') mlab.show() ``` 五、总结 在本篇文章中,我们介绍了如何安装Mayavi库,并提供了一些常见的Python 3D实例集锦。Python 3D可视化技术是一个强大的工具,它为科学和工程应用提供了很多的帮助。如果你正在寻找一个易于使用且功能强大的3D可视化库,那么Mayavi是绝对值得尝试的。

相关标签:

免责声明

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱290110527@qq.com删除。

其他信息

其他资源

Top