首页 > 编程笔记 > Python笔记 阅读:12

seaborn regplot()函数:绘制线性回归图

Python 中,可以通过 seaborn 模块中的 regplot() 函数绘制线性回归图。

regplot() 函数的语法格式如下:

regplot(data,x,y)

其中,参数 data 表示数据集;参数 x 表示 x 轴对应的数据;参数 y 表示 y 轴对应的数据。

示例代码如下:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#设置背景类型
sns.set_style('darkgrid')
#显示中文
plt.rcParams['font.sans-serif']='SimHei'
#显示负号
plt.rcParams['axes.unicode_minus']=False
#创建画布
plt.figure(figsize=(10,8))
#数据集
df_data=pd.read_csv('tips.csv')
#绘制线性回归图
sns.regplot(x='total_bill',y='tip',data=df_data)
上面代码的运行结果如下图所示:


图 1 线性回归图

相关文章