• 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
  • 相关阅读:
    html大作业【NBA篮球介绍 22个页面】学生网页设计源码
    CSS之flex布局
    用于时间触发的嵌入式软件的IDE
    Google Chrome(谷歌浏览器)安装使用
    vision transformer的位置编码总结
    案例分享|金融业数据运营运维一体化建设
    JAVA异常类(简单明了)
    Android 图片加载框架Glide源码详解
    LeetCode·738.单调递增的数字·贪心
    Ubuntu24多版本python解释器使用
  • 原文地址:https://blog.csdn.net/dujidan/article/details/133994993