Python 线性回归可视化 并将回归函数放置到图像上

python 复制代码
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

相关推荐
cnxy18839 分钟前
Python爬虫进阶:反爬虫策略与Selenium自动化完整指南
爬虫·python·selenium
用户8356290780511 小时前
Python 实现 Excel 条件格式自动化
后端·python
深蓝电商API2 小时前
Scrapy管道Pipeline深度解析:多方式数据持久化
爬虫·python·scrapy
噎住佩奇2 小时前
(Win11系统)搭建Python爬虫环境
爬虫·python
basketball6162 小时前
python 的对象序列化
开发语言·python
rgeshfgreh3 小时前
Python流程控制:从条件到循环实战
前端·数据库·python
luoluoal3 小时前
基于python大数据的电影市场预测分析(源码+文档)
python·mysql·django·毕业设计·源码
幻云20103 小时前
Python深度学习:从入门到实战
人工智能·python
Zoey的笔记本4 小时前
敏捷与稳定并行:Scrum看板+BPM工具选型指南
大数据·前端·数据库·python·低代码
开开心心就好5 小时前
图片格式转换工具,右键菜单一键转换简化
linux·运维·服务器·python·django·pdf·1024程序员节