python 2组list绘制拟合曲线、计算拟合方程 R^2

import matplotlib.pyplot as plt
import numpy as np
# plt.rcParams['font.family'] = 'SimHei' # 指定使用中文字体,例如宋体(SimHei)

def Curve_Fitting(x, y, deg):
    parameter = np.polyfit(x, y, deg)    #拟合deg次多项式
    p = np.poly1d(parameter)             #拟合deg次多项式
    aa=''                               #方程拼接  ——————————————————
    for i in range(deg+1): 
        bb=round(parameter[i], 2)
        if bb>0:
            if i==0:
                bb=str(bb)
            else:
                bb='+'+str(bb)
        else:
            bb=str(bb)
        if deg==i:
            aa=aa+bb
        else:
            aa=aa+bb+'x^'+str(deg-i)    #方程拼接  ——————————————————
    # 利用相关系数矩阵计算R方
    r2 = round(np.corrcoef(y, p(x))[0,1]**2,2)
    plt.scatter(x, y, label="Comparison", color='b', marker='o')     #原始数据散点图
    plt.plot(x, p(x), color='r')  # 画拟合曲线
   # plt.text(-1,0,aa,fontdict={'size':'10','color':'b'})
    plt.legend([f"R^2: {r2}", f"Fitting Equation: {aa}"])   #拼接好的方程和R方放到图例
    # 添加标签和标题
    plt.xlabel("Old Normalized_TMB Values")
    plt.ylabel("New Normalized_TMB Values")
    plt.title("Comparison of Normalized_TMB")
    plt.savefig('./tmp.jpg')
    plt.show()
    print('曲线方程为:',aa)
    
    print('     r^2为:', r2)


def plot_scatter(old_list, new_list):
    # 示例数据:相同样本的两次运行结果
    # sample_names = ["Sample 1", "Sample 2", "Sample 3", "Sample 4"]
    run1_values = old_list  # 第一次运行的数据
    run2_values = new_list  # 第二次运行的数据
    # 创建散点图
    plt.scatter(run1_values, run2_values, label="Comparison", color='b', marker='o')
    # 添加标签和标题
    plt.xlabel("Old Normalized_TMB Values")
    plt.ylabel("New Normalized_TMB Values")
    plt.title("Comparison of Normalized_TMB")
    # 添加数据点的标签
    # for i, sample_name in enumerate(sample_names):
    #     plt.annotate(sample_name, (run1_values[i], run2_values[i]))
    # 添加一条线表示一对一的关系
    plt.plot([min(run1_values), max(run1_values)], [min(run1_values), max(run1_values)], linestyle='--', color='r', label="1:1 Line")
    # 显示图例
    plt.legend()
    plt.savefig('./tmp.jpg')
    # 显示散点图
    plt.show()





normalzed_tmb_compare_file = 'normalzed_tmb.txt'
old_FFPE_list, new_FFPE_list = [], []
old_CF_list, new_CF_list = [], []
with open(normalzed_tmb_compare_file) as f:
    next(f)
    for line in f:
        line_list = line.strip('\n').split('\t')
        # if float(line_list[0]) > 20:
        #     continue
        old, type_, new = line_list
        if type_ == 'FFPE':
            old_FFPE_list.append(float(line_list[0]))
            new_FFPE_list.append(float(line_list[-1]))
        if type_ == 'CF':
            old_CF_list.append(float(line_list[0]))
            new_CF_list.append(float(line_list[-1])) 
    # Curve_Fitting(old_FFPE_list, new_FFPE_list, 1)
    # Curve_Fitting(old_CF_list, new_CF_list, 1)
    Curve_Fitting(old_CF_list+old_FFPE_list, new_CF_list+new_FFPE_list, 1)
    # plot_scatter(old_list, new_list)

你可能感兴趣的:(画图,Python,python)