zxcKAMEHAME 2023-03-20 04:49 采纳率: 17.6%
浏览 46
已结题

二维矩阵线性插值问题 python

我有一串气温资料,要用线性插值找出其他时间点的气温,要怎么做
我想把这资料做完线性插值弄成图片
以下是我网上找来找去的自己打出来的,还是有错

import numpy as np
from scipy.interpolate import interp2d
from scipy.interpolate import griddata
# 創建一個547*638的矩陣
matrix = np.zeros((547, 638))


# 定義矩陣大小
rows, cols = 547, 638
# 創建一個新的空矩陣
matrix_new = np.zeros((rows, cols))

# 填充矩陣 設定氣溫

matrix[0,0]=15
matrix[0,91]=15
matrix[0,182]=14
matrix[0,273]=18
matrix[0,364]=24
matrix[0,455]=24
matrix[0,546]=20
matrix[0,637]=17

matrix[91,0]=17
matrix[91,91]=16
matrix[91,182]=15
matrix[91,273]=18
matrix[91,364]=23
matrix[91,455]=23
matrix[91,546]=19
matrix[91,637]=17

matrix[182,0]=17
matrix[182,91]=16
matrix[182,182]=15
matrix[182,273]=18
matrix[182,364]=21
matrix[182,455]=21
matrix[182,546]=18
matrix[182,637]=16

matrix[273,0]=0
matrix[273,91]=0
matrix[273,182]=14
matrix[273,273]=0
matrix[273,364]=21
matrix[273,455]=0
matrix[273,546]=18
matrix[273,637]=0

matrix[364,0]=0
matrix[364,91]=0
matrix[364,182]=14
matrix[364,273]=0
matrix[364,364]=23
matrix[364,455]=0
matrix[364,546]=18
matrix[364,637]=0

matrix[455,0]=0
matrix[455,91]=0
matrix[455,182]=14
matrix[455,273]=0
matrix[455,364]=23
matrix[455,455]=0
matrix[455,546]=19
matrix[455,637]=0

matrix[546,0]=0
matrix[546,91]=0
matrix[546,182]=15
matrix[546,273]=0
matrix[546,364]=23
matrix[546,455]=0
matrix[546,546]=19
matrix[546,637]=0

# 將原本有值的位置填入新矩陣
matrix_new[::91, ::91] = matrix[::91, ::91]
# 定義 x 和 y 的範圍
x_range = np.arange(0, cols)
y_range = np.arange(0, rows)

# 對每五格進行內插
x_new=0
y_new=0
def new_func(matrix_new, x_new, y_new, pts, vals):
    matrix_new[y_new, x_new] = griddata(pts, vals, (x_new, y_new), method='linear', fill_value=0)

for x in range(0, cols, 5):
    for y in range(0, rows, 5):
        # 如果原本位置的值>0,就不做內插
        if matrix_new[y, x] > 0 or y_new == 545:
            continue
        # 要進行內插的新位置
        ####x_new, y_new = int(x+2.5), int(y+2.5)
        y_new = y_new + 5
        # 取出內插需要用到的點
        pts = [(x0, y0) for x0 in x_range for y0 in y_range if matrix_new[y0, x0] > 0]
        # 取出內插需要用到的值
        vals = [matrix_new[y0, x0] for x0, y0 in pts]
        # 進行內插
        new_func(matrix_new, x_new, y_new, pts, vals)
    y_new = 0
    x_new = x_new + 5
    if x_new == 640:
        continue


# 開啟檔案
with open("output.txt", "w") as f:
    # 將矩陣中的每個元素寫入檔案
    for row in matrix_new:
        for value in row:
            f.write(str(value) + " ")
        f.write("\n")
# 打印矩陣a
#print(matrix_new)

  • 写回答

3条回答 默认 最新

  • lshen01 2023-03-20 07:12
    关注

    参考GPT和自己的思路:

    对于您的问题,可以使用插值函数进行二维线性插值。以下是一份Python代码,可以根据您提供的原始数据进行线性插值并绘制图像:

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.interpolate import interp2d
    
    # 构造原始数据
    x = [0, 91, 182, 273, 364, 455, 546, 637]
    y = [0, 91, 182, 273, 364, 455, 546]
    z = [[15,    15, 14, 18, 24, 24, 20, 17],
         [17,    16, 15, 18, 23, 23, 19, 17],
         [17,    16, 15, 18, 21, 21, 18, 16],
         [0,     0,  14, 0,  21, 0,  18, 0],
         [0,     0,  14, 0,  23, 0,  18, 0],
         [0,     0,  14, 0,  23, 0,  19, 0],
         [0,     0,  15, 0,  23, 0,  19, 0]]
    
    # 构建二维线性插值函数
    f = interp2d(x, y, z, kind='linear')
    
    # 构建新的 x,y 范围
    x_new = np.arange(0, 638)
    y_new = np.arange(0, 547)
    
    # 进行插值
    z_new = f(x_new, y_new)
    
    # 绘制插值后的图像
    plt.imshow(z_new, cmap='hot', interpolation='nearest')
    plt.show()
    

    注:请确保安装有NumPy和Matplotlib包。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月30日
  • 已采纳回答 3月22日
  • 修改了问题 3月20日
  • 创建了问题 3月20日

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因