Python——根据散点数据绘制三维曲面图( meshgrid函数以及Axes3D [plot_surface] )

版权声明:此文为笔者原创,如需转载请联系笔者:[email protected] https://blog.csdn.net/Scc_hy/article/details/81455795

1、初始散点数据处理成xy网格数据

import numpy as np
x = np.arange(-2, 2, 0.025)
y = np.arange(-2, 2, 0.025)
x, y = np.meshgrid(x, y)

网格处理过程:

# x 和 Y 均为:
array([-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5])

# meshgrid 函数后
## x
array([[-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5],
       [-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5],
       [-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5],
       [-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5],
       [-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5],
       [-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5],
       [-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5],
       [-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5]])
## y
array([[-2. , -2. , -2. , -2. , -2. , -2. , -2. , -2. ],
       [-1.5, -1.5, -1.5, -1.5, -1.5, -1.5, -1.5, -1.5],
       [-1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. ],
       [-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5],
       [ 1. ,  1. ,  1. ,  1. ,  1. ,  1. ,  1. ,  1. ],
       [ 1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5]])

图示处理过程:

# 1.处理前数据散点分布情况
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import numpy as np
x = np.arange(-2, 2, 0.5)
y = np.arange(-2, 2, 0.5)
plt.plot(x, y)
plt.show()

# 2.处理后数据散点分布情况
## 2.1 平面展示
x, y = np.meshgrid(x, y)
plt.scatter(x, y)
plt.show()

## 2.2 3D展示
import mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)

z = x*y*0
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=plt.cm.jet)
plt.show()

图示:
1.处理前数据散点分布情况
这里写图片描述
2.处理后数据散点分布情况
2.1 平面展示
这里写图片描述
2.2 3D展示
这里写图片描述

2、绘制三维曲面

# 3D绘图示意
import mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

x = np.arange(-4, 4, 0.25)
y = np.arange(-4, 4, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)

ax.plot_surface(x, y, z, rstride = 1,   # row 行步长
                 cstride = 2,           # colum 列步长
                 cmap=plt.cm.hot )      # 渐变颜色
ax.contourf(x, y, z, 
            zdir='z',  # 使用数据方向
            offset=-2, # 填充投影轮廓位置
            cmap=plt.cm.hot)
ax.set_zlim(-2, 2)

plt.show()
图示:
1. rstride = 2,cstride = 1
2. rstride = 1,cstride = 1
3. rstride = 1,cstride = 2
4. rstride = 2,cstride = 2
这里写图片描述

猜你喜欢

转载自blog.csdn.net/Scc_hy/article/details/81455795
今日推荐