matlab绘制立体图形的绘图命令(用Python绘制三维图形更方便)

Python提供了强大的绘图工具包matplotlib,不仅可以绘制常规图形,还可以绘制3D图形,绘制3D图像还需要再安装mpl_toolkits工具包。

以下简单展示几个利用Python绘制的三维图形,更多用法和示例可以去matplotlib官网查看。

三维曲线图

import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') theta = np.linspace(-4 * np.pi, 4 * np.pi, 100) z = np.linspace(-2, 2, 100) r = z**2 1 x = r * np.sin(theta) y = r * np.cos(theta) ax.plot(x, y, z, label='parametric curve') ax.legend() plt.show()

matlab绘制立体图形的绘图命令(用Python绘制三维图形更方便)(1)

三维散点图

import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt #定义了一个生成随机数的函数 def randrange(n, vmin, vmax): return (vmax-vmin)*np.random.rand(n) vmin fig = plt.figure() ax = fig.add_subplot(111, projection='3d') n = 100 for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]: xs = randrange(n, 23, 32) ys = randrange(n, 0, 100) zs = randrange(n, zl, zh) ax.scatter(xs, ys, zs, c=c, marker=m) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show()

matlab绘制立体图形的绘图命令(用Python绘制三维图形更方便)(2)

三维表面图

fig = plt.figure() ax = fig.gca(projection='3d') #绘图数据源 X = np.arange(-5, 5, 0.25) Y = np.arange(-5, 5, 0.25) X, Y = np.meshgrid(X, Y) #meshgrid 函数用来生成网格矩阵,可以是二维网格矩阵,也可以是三维。 R = np.sqrt(X**2 Y**2) Z = np.sin(R) #函数sin(sqrt(x**2 Y**2)) surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False) ax.set_zlim(-1.01, 1.01) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()

matlab绘制立体图形的绘图命令(用Python绘制三维图形更方便)(3)

三维球面

from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) x = 10 * np.outer(np.cos(u), np.sin(v)) y = 10 * np.outer(np.sin(u), np.sin(v)) z = 10 * np.outer(np.ones(np.size(u)), np.cos(v)) ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b') plt.show()

matlab绘制立体图形的绘图命令(用Python绘制三维图形更方便)(4)

莫比乌斯带

import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import matplotlib.tri as mtri # u, v are parameterisation variables u = (np.linspace(0, 2.0 * np.pi, endpoint=True, num=50) * np.ones((10, 1))).flatten() v = np.repeat(np.linspace(-0.5, 0.5, endpoint=True, num=10), repeats=50).flatten() # This is the Mobius mapping, taking a u, v pair and returning an x, y, z # triple x = (1 0.5 * v * np.cos(u / 2.0)) * np.cos(u) y = (1 0.5 * v * np.cos(u / 2.0)) * np.sin(u) z = 0.5 * v * np.sin(u / 2.0) # Triangulate parameter space to determine the triangles tri = mtri.Triangulation(u, v) fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection='3d') # The triangles in parameter space determine which x, y, z points are # connected by an edge ax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap=plt.cm.Spectral) ax.set_zlim(-1, 1) # First create the x and y coordinates of the points. n_angles = 36 n_radii = 8 min_radius = 0.25 radii = np.linspace(min_radius, 0.95, n_radii) angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False) angles = np.repeat(angles[...,np.newaxis], n_radii, axis=1) angles[:,1::2] = np.pi/n_angles x = (radii*np.cos(angles)).flatten() y = (radii*np.sin(angles)).flatten() z = (np.cos(radii)*np.cos(angles*3.0)).flatten() # Create the Triangulation; no triangles so Delaunay triangulation created. triang = mtri.Triangulation(x, y) # Mask off unwanted triangles. xmid = x[triang.triangles].mean(axis=1) ymid = y[triang.triangles].mean(axis=1) mask = np.where(xmid*xmid ymid*ymid < min_radius*min_radius, 1, 0) triang.set_mask(mask) plt.show()

matlab绘制立体图形的绘图命令(用Python绘制三维图形更方便)(5)

感谢观看,喜欢的朋友,关注走一波,后续内容更精彩!

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页