This article will record some interesting 3D pictures for a long time
1. Drawing of graphs and scatter plots
1. Drawing of graphs and scatter plots
import numpy as np
import matplotlib.pyplot as plt
# 创建一个3d坐标系
fig = plt.figure()
ax = fig.gca(projection = '3d')
help(plt.plot)
help(np.random.sample)
# 利用x轴和y轴绘制sin曲线
x = np.linspace(0, 1, 100) # linspace创建等差数组
y = np.cos(x * 2 * np.pi) / 2 + 0.5
# 通过zdir = 'z' 将数据绘制在z轴,zs = 0.5 则是将数据绘制在z = 0.5的地方
ax.plot(x, y, zs = 0.5, zdir = 'z', color = 'black', label = 'curve in (x, y)')
# 绘制散点数据 (每个颜色20个2D点)在x轴和z轴
colors = ('r', 'g', 'b', 'k')
np.random.seed(19680801) # 设置随机函数复现
x = np.random.sample(20 * len(colors))
y = np.random.sample(20 * len(colors))
z = np.random.sample(20 * len(colors))
c_list = []
for i in colors:
c_list.extend([i] * 20)
# 绘制散点坐标 通过zdir = 'y' 将数据绘制在y为 0 的地方
ax.scatter(x, y, z, zdir = 'y', c = c_list, label = 'point in (x, z)')
# 设置图例
ax.legend()
#限制个轴的范围
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
# 轴添加标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()