Python 散点图 回归拟合 带误差 各参数详解(2)



1.数据下载地址

散点图样例数据:https://download.csdn.net/download/qq_35240689/87006447

Python 散点图 回归拟合 带误差 各参数详解(2)_第1张图片
Python 散点图 回归拟合 带误差 各参数详解(2)_第2张图片

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats

scatter_data = pd.read_excel(r"散点图样例数据.xlsx")
x = scatter_data["values"]
y = scatter_data["pred values"]

slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)

#绘制1:1线
best_line_x = np.linspace(-10,10)
best_line_y=best_line_x
# 拟合线
y3 = slope*x + intercept
# RMSE
fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
scatter = ax.scatter(x=x,y=y,edgecolor=None, c='k', s=20,marker='s',label="Data")
bestline = ax.plot(best_line_x,best_line_y,color='k',linewidth=1.5,linestyle='--',label="1:1 Line")
linreg = ax.plot(x,y3,color='r',linewidth=1.5,linestyle='-',label="Fitted Line")
ax.set_xlim((-.1, 1.8))
ax.set_ylim((-.1, 1.8))
ax.set_xticks(np.arange(0, 2, step=0.2))
ax.set_yticks(np.arange(0, 2, step=0.2))
ax.grid(False)

# 添加文本信息
fontdict = {"size":13,"fontstyle":"italic"}
ax.text(0.,1.6,r'$R=$'+str(round(r_value,2)),fontdict=fontdict)
ax.text(0.,1.4,"$P <$ "+str(0.001),fontdict=fontdict)
ax.text(0.,1.2,r'$y=$'+str(round(slope,3))+'$x$'+" + "+str(round(intercept,3)),fontdict=fontdict)
ax.text(0.,1.0,r'$N=$'+ str(len(x)),fontdict=fontdict)
ax.set_xlabel("Variable 01")
ax.set_ylabel("Variable 02")
ax.legend(loc="lower right")

plt.tight_layout()
# fig.savefig('散点图_cor_error.pdf',bbox_inches='tight')


2. 带误差

添加 xerr=x_err,yerr=y_err

Python 散点图 回归拟合 带误差 各参数详解(2)_第3张图片

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats

scatter_data = pd.read_excel(r"散点图样例数据.xlsx")
x = scatter_data["values"]
y = scatter_data["pred values"]
x_err = scatter_data["x_error"]
y_err = scatter_data["y_error"]

slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)

#绘制1:1拟合线
best_line_x = np.linspace(-10,10)
best_line_y=best_line_x
#绘制拟合线
y3 = slope*x + intercept
#开始绘图
fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
scatter = ax.scatter(x=x,y=y,edgecolor=None, c='k', s=20,label="Data")
bestline = ax.plot(best_line_x,best_line_y,color='k',linewidth=1.5,linestyle='--',label="1:1 Line")
linreg = ax.plot(x,y3,color='r',linewidth=1.5,linestyle='-',label="Fitted Line")
# 添加误差线
errorbar = ax.errorbar(x,y,xerr=x_err,yerr=y_err,ecolor="k",
                       elinewidth=.4,capsize=0,alpha=.4,
                       linestyle="",mfc="none",mec="none",zorder=-1)
ax.set_xlim((-.1, 1.8))
ax.set_ylim((-.1, 1.8))
ax.set_xticks(np.arange(0, 2, step=0.2))
ax.set_yticks(np.arange(0, 2, step=0.2))
# 添加文本信息
fontdict = {"size":13,"fontstyle":"italic"}
ax.text(0.,1.6,r'$R=$'+str(round(r_value,2)),fontdict=fontdict)
ax.text(0.,1.4,"$P <$ "+str(0.001),fontdict=fontdict)
ax.text(0.,1.2,r'$y=$'+str(round(slope,3))+'$x$'+" + "+str(round(intercept,3)),fontdict=fontdict)
ax.text(0.,1.0,r'$N=$'+ str(len(x)),fontdict=fontdict)

ax.set_xlabel("Variable 01")
ax.set_ylabel("Variable 02")
ax.legend(loc="lower right")
ax.grid(False)
plt.tight_layout()
# fig.savefig('散点图_cor_error.pdf',bbox_inches='tight')

你可能感兴趣的:(python,数据可视化,python,回归,开发语言)