1、初始散点数据处理成xy网格数据
import numpy as npx = 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 函数后## xarray([[-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]])## yarray([[-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 pltplt.style.use('ggplot')import numpy as npx = 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 Axes3Dfig = plt.figure()ax = Axes3D(fig)z = x*y*0ax.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 Axes3Dfig = 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
如果觉得《Python——根据散点数据绘制三维曲面图( meshgrid函数以及Axes3D [plot_surface] )》对你有帮助,请点赞、收藏,并留下你的观点哦!