python将写数组 txt, 读数组并画图

写数组 data.txt

sdf 是一个tensor,其维度(128,1),每次在txt写一个数,用空格隔开,写完一行之后进行换行。

 col = sdf.shape[0]  ## txt 文件的 col = 128
        with open(os.path.join(img_dir,"data.txt"),'w') as f:
            for i in range(col):
                f.write(str(sample_t.detach().cpu().numpy().squeeze()[i]) + ' ')
            f.write("\n")

data.txt 文件里面数字按行写入。

将data.txt的数字读出来,并进行绘图显示

import os
import matplotlib.pyplot as plt
import numpy as np


base_dir = "pixel_plot"

index = 1
def Neus_compare(data_orign = None, data_laplace = None):
    global index
    t_laplace = data_laplace[0]
    sdf_laplace = data_laplace[1]
    alpha_laplace = data_laplace[2]
    T_laplace = data_laplace[3]
    weight_laplace = data_laplace[4]

    ## plot sdf
    plt.title(f'sdf_compare_point{index}')
    plt.plot(t_laplace,sdf_laplace,label='laplace')
    plt.plot(data_orign[0],data_orign[1],color='red',linestyle='--',label='orign')
    plt.legend()
    plt.savefig(os.path.join(os.path.join(base_dir,f"pixel{index}"),"sdf.png"))
    plt.clf()

    index += 1

for i in sorted(os.listdir(base_dir)):
    data_laplace = []
    data_orign = []

    with open(os.path.join(base_dir,os.path.join(i,'data.txt'))) as f:
        lines = f.readlines()
        for line in lines:
            lineData = line.strip().split()
            lineData = np.array(lineData).astype(float).tolist()
            data_laplace.append(lineData)

    Neus_compare(data_orign=data_orign,data_laplace=data_laplace)  ## 绘图函数

你可能感兴趣的:(python,numpy,开发语言)