tft每日頭條

 > 生活

 > matlab繪制立體圖形的繪圖命令

matlab繪制立體圖形的繪圖命令

生活 更新时间:2024-07-21 12:19:01

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

感謝觀看,喜歡的朋友,關注走一波,後續内容更精彩!

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved