11 Python Matplotlib中一些有意思的3D图形绘制

本篇将会长期记录一些有意思的3D图

1. 曲线图和散点图的绘制
2. 3D立体像素图的绘制
3.洛伦兹吸引子(Lorenz Attractor)

1. 曲线图和散点图的绘制

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()
最终效果:

11 Python Matplotlib中一些有意思的3D图形绘制_第1张图片

2. 3D立体像素图的绘制

import matplotlib.pyplot as plt
import numpy as np

def explode(data):
  size = np.array(data.shape)*2
  data_e = np.zeros(size - 1, dtype = data.dtype)
  data_e[::2, ::2, ::2] = data
  return data_e


nvoxels = np.zeros((4, 4, 4), dtype = bool)
nvoxels[1, 1, 3] = True
nvoxels[1, 2, 3] = True
nvoxels[2, 1, 3] = True
nvoxels[2, 2, 3] = True

nvoxels[1, 1, 0] = True
nvoxels[1, 2, 0] = True
nvoxels[2, 1, 0] = True
nvoxels[2, 2, 0] = True

nvoxels[1, 0, 2] = True
nvoxels[2, 0, 2] = True
nvoxels[1, 0, 1] = True
nvoxels[2, 0, 1] = True

nvoxels[1, 3, 2] = True
nvoxels[2, 3, 2] = True
nvoxels[1, 3, 1] = True
nvoxels[2, 3, 1] = True

nvoxels[3, 1, 1] = True
nvoxels[3, 1, 2] = True
nvoxels[3, 2, 1] = True
nvoxels[3, 2, 2] = True

nvoxels[0, 1, 1] = True
nvoxels[0, 1, 2] = True
nvoxels[0, 2, 1] = True
nvoxels[0, 2, 2] = True

facecolors = np.where(nvoxels, 'violet', 'cyan')
edgecolors = np.where(nvoxels, '#BFAB6E', 'cadetblue')
filled = np.ones(nvoxels.shape)

filled_2 = explode(filled)
fcolors_2 = explode(facecolors)
ecolors_2 = explode(edgecolors)

x, y, z = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2
x[0::2, :, :] += 0.05
y[:, 0::2, :] += 0.05
z[:, :, 0::2] += 0.05
x[1::2, :, :] += 0.95
y[:, 1::2, :] += 0.95
z[:, :, 1::2] += 0.95


fig = plt.figure()
ax = fig.gca(projection = '3d')
ax.voxels(x, y, z, filled_2, facecolors = fcolors_2, edgecolors = ecolors_2)
plt.show()

11 Python Matplotlib中一些有意思的3D图形绘制_第2张图片

3.洛伦兹吸引子(Lorenz Attractor)

import numpy as np
import matplotlib.pyplot as plt

def lorenz(x, y, z, s = 10, r = 28, b = 2.667):
  x_dot = s*(y - x)
  y_dot = r * x - y - x * z
  z_dot = x * y - b * z
  return x_dot, y_dot, z_dot

# 为每个轴的数组第一个数,设置一个初始值
dt = 0.019
num_steps = 2000

xs = np.empty(num_steps + 1 )
ys = np.empty(num_steps + 1)
zs = np.empty(num_steps + 1)
# np.set_printoptions(threshold = 1000000)
xs[0], ys[0], zs[0] = (0., 1., 1.05)

# 计算当前点的偏导数,然后根据这个点推导出下一个点
for i in range(num_steps):
    x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
    xs[i + 1] = xs[i] + (x_dot * dt)
    ys[i + 1] = ys[i] + (y_dot * dt)
    zs[i + 1] = zs[i] + (z_dot * dt)
 
# 绘制坐标
fig = plt.figure()
ax = fig.gca(projection = '3d')

ax.plot(xs, ys, zs, c = 'm', alpha = 0.6,lw = 0.8)
print(help(plt.plot))
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
ax.set_title("Lorrenz")
plt.show()

<>
11 Python Matplotlib中一些有意思的3D图形绘制_第3张图片

你可能感兴趣的:(Python,Matplotlib)