mpl_toolkits.mplot3d是Matplotlib里面专门用来画三维图的工具包,官方指南请点击此处《mplot3d tutorial》
引入及配置
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
mpl.rcParams['legend.fontsize'] = 20 # mpl模块载入的时候加载配置信息存储在rcParams变量中,rc_params_from_file()函数从文件加载配置信息
font = {
'color': 'b',
'style': 'oblique',
'size': 20,
'weight': 'bold'
}
fig = plt.figure(figsize=(16, 12)) #参数为图片大小
ax = fig.gca(projection='3d') # get current axes,且坐标轴是3d的
ax.set_aspect('equal') # 坐标轴间比例一致
应用
1、三维空间中的点
n = 100
def randrange(n, vmin, vmax):
r = np.random.rand(n) # 随机生成n个介于0~1之间的数
return (vmax - vmin) * r + vmin # 得到n个[vmin,vmax]之间的随机数
for zlow, zhigh, c, m, l in [(4, 15, 'r', 'o', 'positive'), (13, 40, 'g', '*', 'negative')]: # 用两个tuple,是为了将形状和颜色区别开来
x = randrange(n, 15, 40)
y = randrange(n, -5, 25)
z = randrange(n, zlow, zhigh)
ax.scatter(x, y, z, c=c, marker=m, label=l, s=z * 10) #这里marker的尺寸和z的大小成正比
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.set_zlabel("Z axis")
ax.set_title("Scatter plot", alpha=0.6, color="b", size=25, weight='bold', backgroundcolor="y") #子图的title
ax.legend(loc="upper left") #legend的位置左上
plt.show()
2、三维空间中的线
# 准备数据
theta = np.linspace(-8 * np.pi, 8 * np.pi, 100) # 生成等差数列,[-8π,8π],个数为100
z = np.linspace(-2, 2, 100) # [-2,2]容量为100的等差数列,这里的数量必须与theta保持一致,因为下面要做对应元素的运算
r = z ** 2 + 1
x = r * np.sin(theta) # [-5,5]
y = r * np.cos(theta) # [-5,5]
ax.set_xlabel("X", fontdict=font)
ax.set_ylabel("Y", fontdict=font)
ax.set_zlabel("Z", fontdict=font)
ax.set_title("Line Plot", alpha=0.5, fontdict=font) #alpha参数指透明度transparent
ax.plot(x, y, z, label='parametric curve')
ax.legend(loc='upper right') #legend的位置可选:upper right/left/center,lower right/left/center,right,left,center,best等等
plt.show()
3、面
from matplotlib.ticker import LinearLocator, FormatStrFormatter
# 准备数据
x = np.arange(-5, 5, 0.25) #生成[-5,5]间隔0.25的数列,间隔越小,曲面越平滑
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x,y) #格点矩阵,原来的x行向量向下复制len(y)次,形成len(y)*len(x)的矩阵,即为新的x矩阵;原来的y列向量向右复制len(x)次,形成len(y)*len(x)的矩阵,即为新的y矩阵;新的x矩阵和新的y矩阵shape相同
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(r)
surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm) # cmap指color map
# 自定义z轴
ax.set_zlim(-1, 1)
ax.zaxis.set_major_locator(LinearLocator(20)) # z轴网格线的疏密,刻度的疏密,20表示刻度的个数
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # 将z的value字符串转为float,保留2位小数
#设置坐标轴的label和标题
ax.set_xlabel('x',size=15)
ax.set_ylabel('y',size=15)
ax.set_zlabel('z',size=15)
ax.set_title("Surface plot", weight='bold', size=20)
#添加右侧的色卡条
fig.colorbar(surf, shrink=0.6, aspect=8) # shrink表示整体收缩比例,aspect仅对bar的宽度有影响,aspect值越大,bar越窄
plt.show()
4、体
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
# 六面体顶点和面
verts = [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0), (0, 0, 1), (0, 1, 1), (1, 1, 1), (1, 0, 1)]
faces = [[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [0, 3, 7, 4]]
# 四面体顶点和面
# verts = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 0, 1)]
# faces = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]]
# 获得每个面的顶点
poly3d = [[verts[vert_id] for vert_id in face] for face in faces]
# 绘制顶点
x, y, z = zip(*verts)
ax.scatter(x, y, z)
# 绘制多边形面
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='w', linewidths=1, alpha=0.3))
# 绘制多边形的边
ax.add_collection3d(Line3DCollection(poly3d, colors='k', linewidths=0.5, linestyles=':'))
# 设置图形坐标范围
ax.set_xlabel('X')
ax.set_xlim3d(-0.5, 1.5)
ax.set_ylabel('Y')
ax.set_ylim3d(-0.5, 1.5)
ax.set_zlabel('Z')
ax.set_zlim3d(-0.5, 1.5)
plt.show()
网友评论