import matplotlib.pyplot as plt
import scipy
import seaborn as sns# 加载内置的数据集
df = sns.load_dataset('tips')#create regplot
p = sns.regplot(x='total_bill', y='tip', data=df)#calculate slope and intercept of regression equation
slope, intercept, r, p, sterr = scipy.stats.linregress(x=p.get_lines()[0].get_xdata(),y=p.get_lines()[0].get_ydata())#add regression equation to plot
result_string = f"$y = {slope:.2f}x + {intercept:.2f}$"plt.figtext(0.5, 0.6, result_string)plt.show()
运行结果如下:
2024年2月7日22:33:05