我有一串气温资料,要用线性插值找出其他时间点的气温,要怎么做
我想把这资料做完线性插值弄成图片
以下是我网上找来找去的自己打出来的,还是有错
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)
参考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包。
参考GPT和自己的思路:
根据您提供的代码,您已经成功地创建了一个547*638的矩阵,并填充了一些温度值。接下来,您需要进行线性插值以找到其他时间点的温度值。您可以使用SciPy库的griddata函数进行插值。
您需要定义一个用于插值的网格点和温度值的数组,然后使用griddata函数来插值。代码应如下所示:
# 定义网格点坐标
x = np.arange(0, cols)
y = np.arange(0, rows)
# 获取网格点上的温度值
z = matrix.ravel()
# 进行插值
interp = griddata((x, y), z, (xx, yy), method='linear')
在这里,xx和yy是您想要获取温度的时间点的坐标。可以通过np.meshgrid函数创建这些坐标。
最后,您需要将插值结果保存到一个图像文件中。您可以使用matplotlib库来完成这一操作。代码如下所示:
import matplotlib.pyplot as plt
# 绘制插值结果
plt.imshow(interp, cmap='coolwarm')
plt.savefig('temperature.png')
这将生成一个名为“temperature.png”的图像文件,其中包含所插值的温度数据。