1 三维图像的绘制简介
Matplotlib使用和绘制二维图表:https://blog.csdn.net/ZGL_cyy/article/details/125705060
matplotlib支持绘制三维线框图, 三维曲面图, 三维散点图. 需要使用axes3d提供3d坐标系.
from mpl_toolkits.mplot3d import axes3d ax3d = mp.gca(projection='3d') ax3d.plot_wireframe() # 绘制3d线框图 ax3d.plot_surface() # 绘制3d曲面图 ax3d.scatter() # 绘制3d散点图
2 三维线框图
ax3d.plot_wireframe( x, y, # x,y网格点坐标矩阵 z, # z为每个坐标点的值 rstride=30, # 行跨距 cstride=30, # 列跨距 linewidth=1, color='' )
案例演示:
""" 三维线框图 """ import numpy as np import matplotlib.pyplot as mp from mpl_toolkits.mplot3d import axes3d # 生成网格点坐标矩阵 n = 1000 x, y = np.meshgrid(np.linspace(-3, 3, n), np.linspace(-3, 3, n)) # 根据x,y 计算当前坐标下的z高度值 z = (1-x/2 + x**5 + y**3) * np.exp(-x**2 -y**2) mp.figure('Wireframe', facecolor='lightgray') ax3d = mp.gca(projection='3d') ax3d.set_xlabel('X', fontsize=14) ax3d.set_ylabel('Y', fontsize=14) ax3d.set_zlabel('Z', fontsize=14) ax3d.plot_wireframe(x, y, z, rstride=10, cstride=10,color='dodgerblue') mp.show()
3 三维曲面图
ax3d.plot_surface( x, y, # x,y网格点坐标矩阵 z, # z为每个坐标点的值 rstride=30, # 行跨距 cstride=30, # 列跨距 cmap='jet' )
案例演示:
""" 三维曲面图 """ import numpy as np import matplotlib.pyplot as mp from mpl_toolkits.mplot3d import axes3d # 生成网格点坐标矩阵 n = 1000 x, y = np.meshgrid(np.linspace(-3, 3, n), np.linspace(-3, 3, n)) # 根据x,y 计算当前坐标下的z高度值 z = (1-x/2 + x**5 + y**3) * np.exp(-x**2 -y**2) mp.figure('Surface', facecolor='lightgray') ax3d = mp.gca(projection='3d') ax3d.set_xlabel('X', fontsize=14) ax3d.set_ylabel('Y', fontsize=14) ax3d.set_zlabel('Z', fontsize=14) ax3d.plot_surface(x, y, z, rstride=50, cstride=50, cmap='jet') mp.show()
4 三维散点图
ax3d.scatter( x, y, z, # x,y,z 确定一组散点坐标 marker='', # 点型 s = 60, # 点的大小 edgecolor='', # 边缘色 facecolor='', # 填充色 zorder=3, # 绘制图层编号 c=d, # 设置过渡性颜色 cmap='jet' # 颜色映射 ) 12345678910
案例演示:
""" 三维散点图 """ import numpy as np import matplotlib.pyplot as mp import mpl_toolkits.mplot3d as axes3d n = 500 x = np.random.normal(0, 1, n) y = np.random.normal(0, 1, n) z = np.random.normal(0, 1, n) d = np.sqrt(x**2 + y**2 + z**2) mp.figure('3D Scatter') ax3d = mp.gca(projection='3d') ax3d.set_xlabel('X', fontsize=14) ax3d.set_ylabel('Y', fontsize=14) ax3d.set_zlabel('Z', fontsize=14) ax3d.scatter(x, y, z, s=60, alpha=0.6, c=d, cmap='jet') mp.show()
5 三维柱状图
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D #构造需要显示的值 X=np.arange(0, 5, step=1)#X轴的坐标 Y=np.arange(0, 9, step=1)#Y轴的坐标 #设置每一个(X,Y)坐标所对应的Z轴的值,在这边Z(X,Y)=X+Y Z=np.zeros(shape=(5, 9)) for i in range(5): for j in range(9): Z[i, j]=i+j xx, yy=np.meshgrid(X, Y)#网格化坐标 X, Y=xx.ravel(), yy.ravel()#矩阵扁平化 bottom=np.zeros_like(X)#设置柱状图的底端位值 Z=Z.ravel()#扁平化矩阵 width=height=1#每一个柱子的长和宽 #绘图设置 fig=plt.figure() ax=fig.gca(projection='3d')#三维坐标轴 ax.bar3d(X, Y, bottom, width, height, Z, shade=True)# #坐标轴设置 ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z(value)') plt.show()
6 三维折线图
import numpy as np import matplotlib.pyplot as plt xs1 = np.ones(100) ys1 = range(1, 101) zs1 = np.zeros(100) xs2 = np.ones(100) * 2 ys2 = range(1, 101) zs2 = np.zeros(100) xs3 = np.ones(100) * 3 ys3 = range(1, 101) zs3 = np.zeros(100) for i in range(21, 100): zs3[i] = -0.22 xs4 = np.ones(100) * 4 ys4 = range(1, 101) zs4 = np.zeros(100) # Plot from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) ax.plot(xs1, ys1, zs1, lw=2, color=[0.8, 0.33, 0]) ax.plot(xs2, ys2, zs2, lw=2, color=[0.33, 0.8, 0]) ax.plot(xs3, ys3, zs3, lw=2, color=[0, 0.5, 0.8]) ax.plot(xs4, ys4, zs4, lw=2, color=[0.8, 0.1, 0.5]) # ax.set_xlabel("X Axis") # ax.set_ylabel("Y Axis") # ax.set_zlabel("Z Axis") # ax.set_title("Addective fault simulation") plt.show()