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

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
这里写图片描述
  • 32
    点赞
  • 138
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Scc_hy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值