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,二维插值,三维
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python列表(list)、字典(dict)、字符串(string)基本操作小结
2023-03-02 01:33:01
SQL语句练习实例之七 剔除不需要的记录行
2011-11-03 16:50:51
轻松接触SQL Server 2000实例的命名规则
2009-01-23 13:44:00
scrapy处理python爬虫调度详解
2021-09-10 11:30:00
如何在MySQL查询结果集中得到记录行号
2008-12-17 15:00:00
Bootbox将后台JSON数据填充Form表单的实例代码
2023-08-22 22:01:21
![](https://img.aspxhome.com/file/2023/0/56090_0s.gif)
Python中re.findall()用法详解
2023-08-09 23:46:14
![](https://img.aspxhome.com/file/2023/1/61191_0s.png)
详解四种Python中基本形态学滤波的实现
2023-05-09 15:10:09
![](https://img.aspxhome.com/file/2023/1/65531_0s.png)
Django 博客实现简单的全文搜索的示例代码
2023-12-07 10:09:19
Python设计模式中的创建型工厂模式
2023-05-09 17:47:02
![](https://img.aspxhome.com/file/2023/2/76132_0s.png)
怎么样才能让层显示在FLASH之上呢
2008-03-05 13:32:00
VBScript中变量作用域
2007-11-02 10:03:00
python实现的DES加密算法和3DES加密算法实例
2023-04-28 07:44:01
详解Python流程控制语句
2021-05-15 08:25:45
解决pip install xxx报错SyntaxError: invalid syntax的问题
2023-02-13 23:53:07
![](https://img.aspxhome.com/file/2023/0/90760_0s.jpg)
如何解决AJAX中跨域访问出现'没有权限'的错误
2008-09-11 17:20:00
pydev使用wxpython找不到路径的解决方法
2023-02-23 05:01:01
Python configparser模块配置文件过程解析
2023-03-04 09:35:49
python中的文件打开与关闭操作命令介绍
2021-07-05 10:15:39
OpenCV 图像梯度的实现方法
2023-07-14 08:25:43
![](https://img.aspxhome.com/file/2023/6/64296_0s.png)