在python中的图形中添加坡度和r^2

2024-09-30 08:36:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个密码

plt.figure()
plt.subplot(2,2,1)
x1 = delta_ST_all.iloc[0:47,15].values.reshape(-1, 1)
y1 = delta_ST_all.iloc[0:47,1].values.reshape(-1, 1)  
linear_regressor = LinearRegression()
linear_regressor.fit(x1, y1) 
y1_pred = linear_regressor.predict(x1)
plt.scatter(x1, y1, label='AE 220-225m', s=20, color='lightpink')
plt.plot(x1, y1_pred, color='lightpink')

但我想添加一个带有坡度和r^2的图例,有人能帮我吗? 我已经能够打印它并看到它,但我找不到代码将它放在图表上的图例中


Tags: pltallcolordeltalinearstvaluesx1
1条回答
网友
1楼 · 发布于 2024-09-30 08:36:04

通过plt.text

plt.text(0.8,0.9,
        'Slope={:.2g}\nR²{:.2g}'.format(float(slope),float(r2)),
        fontsize=16,ha='center')

其中sloper2是您计算的值

然后,文本将被放置在位置x=0.8,y=0.9,其中x和y是数据的值。 如果要将文字放置在相对于x、y轴(单位为正方形)的相对位置,则:

ax=plt.gca()
plt.text(0.8,0.9,
        'Slope={:.2g}\nR²{:.2g}'.format(float(slope),float(r2)),
        transform =ax.transAxes,fontsize=16,ha='center')

此处,文本将位于右上角,其中x=0.8,y=0.9,相对于轴

相关问题 更多 >

    热门问题