python实现二维插值的三维显示
作者:TOliverQueen 时间:2022-05-28 14:17:58
本文实例为大家分享了二维插值的三维显示具体代码,供大家参考,具体内容如下
# -*- coding: utf-8 -*-
"""
演示二维插值。
"""
# -*- coding: utf-8 -*-
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib as mpl
from scipy import interpolate
import matplotlib.cm as cm
import matplotlib.pyplot as plt
def func(x, y):
return (x + y) * np.exp(-5.0 * (x ** 2 + y ** 2))
# X-Y轴分为20*20的网格
x = np.linspace(-1, 1, 20)
y = np.linspace(-1, 1, 20)
x, y = np.meshgrid(x, y) # 20*20的网格数据
fvals = func(x, y) # 计算每个网格点上的函数值 15*15的值
fig = plt.figure(figsize=(9, 6)) #设置图的大小
# Draw sub-graph1
ax = plt.subplot(1, 2, 1, projection='3d') #设置图的位置
surf = ax.plot_surface(x, y, fvals, rstride=2, cstride=2, cmap=cm.coolwarm, linewidth=0.5, antialiased=True) #第四个第五个参数表示隔多少个取样点画一个小面,第六个表示画图类型,第七个是画图的线宽,第八个表示抗锯齿
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f(x, y)') #标签
plt.colorbar(surf, shrink=0.5, aspect=5) # 标注
# 二维插值
newfunc = interpolate.interp2d(x, y, fvals, kind='cubic') # newfunc为一个函数
# 计算100*100的网格上的插值
xnew = np.linspace(-1, 1, 100) # x
ynew = np.linspace(-1, 1, 100) # y
fnew = newfunc(xnew, ynew) # 仅仅是y值 100*100的值 np.shape(fnew) is 100*100
xnew, ynew = np.meshgrid(xnew, ynew)
ax2 = plt.subplot(1, 2, 2, projection='3d')
surf2 = ax2.plot_surface(xnew, ynew, fnew, rstride=2, cstride=2, cmap=cm.coolwarm, linewidth=0.5, antialiased=True)
ax2.set_xlabel('xnew')
ax2.set_ylabel('ynew')
ax2.set_zlabel('fnew(x, y)')
plt.colorbar(surf2, shrink=0.5, aspect=5) # 标注
plt.show()
来源:https://blog.csdn.net/momingqimiao71/article/details/78217109
标签:python,二维插值,三维


猜你喜欢
PyQt与pycharm的结合使用教程
2022-09-08 06:47:05

使用javascript提交form表单方法汇总
2023-08-23 09:03:48
MySQL里Create Index 能否创建主键 Primary Key
2024-01-21 07:12:49
js自定义网页右键菜单方法
2007-11-28 12:50:00
python用分数表示矩阵的方法实例
2023-12-22 21:58:11

sqlserver 多表查询不同数据库服务器上的表
2024-01-15 04:56:24
python中@property和property函数常见使用方法示例
2021-11-13 03:32:20
MySQL获得当前日期时间函数示例详解
2024-01-27 07:54:48
PHP生成饼图的示例代码
2023-05-25 10:24:09
python+selenium实现12306模拟登录的步骤
2021-06-18 15:32:56
python深度学习tensorflow安装调试教程
2021-06-28 23:03:51

一些需要禁用的PHP危险函数(disable_functions)
2023-11-23 15:29:25
asp如何在网站上提供音乐下载?
2010-06-22 21:14:00
Vue动态路由缓存不相互影响的解决办法
2024-05-13 09:44:16
ThinkPHP发送邮件示例代码
2023-11-21 19:17:31

使用Python3编写抓取网页和只抓网页图片的脚本
2023-08-15 08:00:17
JSP组件commons-fileupload实现文件上传
2023-07-03 03:16:01
Tensorflow 自带可视化Tensorboard使用方法(附项目代码)
2023-10-13 00:37:40

简单了解Python多态与属性运行原理
2021-03-13 21:29:42
Python CNN卷积神经网络实战教程深入讲解
2023-09-28 19:51:12
