python seaborn 绘图入门: 回归模型 lmplot

python seaborn 绘图入门 : 回归模型 Lmplot

lmplot

lmplot 是一种集合基础绘图与基于数据建立回归模型的绘图方法。旨在创建一个方便拟合数据集回归模型的绘图方法,利用'hue'、'col'、'row'参数来控制绘图变量。

同时可以使用模型参数来调节需要拟合的模型:order、logistic、lowess、robust、logx。

seaborn.lmplot(x, y, data, hue=None, col=None, row=None, palette=None, col_wrap=None, size=5, aspect=1, markers='o', sharex=True, sharey=True, hue_order=None, col_order=None, row_order=None, legend=True, legend_out=True, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=False, x_jitter=None, y_jitter=None, scatter_kws=None, line_kws=None)

Common Parameters:

hue, col, row: strings #定义数据子集的变量,并在不同的图像子集中绘制

Variables that define subsets of the data, which will be drawn on separate facets in the grid. See the *_order parameters to control the order of levels of this variable.

size: scalar, optional #定义子图的高度

Height (in inches) of each facet. See also: aspect.

markers: matplotlib marker code or list of marker codes, optional #定义散点的图标

Markers for the scatterplot. If a list, each marker in the list will be used for each level of the hue variable.

col_wrap: int, optional #设置每行子图数量

“Wrap” the column variable at this width, so that the column facets span multiple rows. Incompatible with a row facet.

order: int, optional #多项式回归,设定指数

If order is greater than 1, use numpy.polyfit to estimate a polynomial regression.

logistic: bool, optional #逻辑回归

If True, assume that y is a binary variable and use statsmodels to estimate a logistic regression model. Note that this is substantially more computationally intensive than linear regression, so you may wish to decrease the number of bootstrap resamples (n_boot) or set ci to None.

logx: bool, optional #转化为 log(x)

If True, estimate a linear regression of the form y ~ log(x), but plot the scatterplot and regression model in the input space. Note that x must be positive for this to work.

Senior Example Ⅰ for Practice

importseabornassns

sns.set_style("whitegrid")

tips=sns.load_dataset("tips")

#载入自带数据集#研究小费tips与总消费金额total_bill在吸烟与不吸烟人之间的关系

g=sns.lmplot(x="total_bill",y="tip",hue="smoker",data=tips,palette="Set1")

通过回归模型发现 total_bill=20 为分界点,不吸烟者的小费高于吸烟者

#研究在不同星期下,消费总额与消费的回归关系,col|hue控制子图不同的变量day,col_wrap控制每行子图数量,size控制子图高度

g=sns.lmplot(x="total_bill",y="tip",col="day",hue="day",data=tips,col_wrap=2,size=3)

#继续研究pokemon数据集

importpandasaspd

importseabornassns

pokemon=pd.read_csv('H:/zhihu/Pokemon.csv')

pokemon.head()

#观察每一代攻击与防御的分布,利用二次多项式逼近

sns.lmplot(x="Defense",y="Attack",data=pokemon,col="Generation",hue="Generation",col_wrap=3,size=3,order=2)

#继续在同一图中观察不同代的sp.Atk,Sp.Def线性关系

sns.lmplot(x="Sp. Atk",y="Sp. Def",data=pokemon,hue='Generation',size=5,order=1)

sp.Atk,Sp.Def线性相关性不高导致图像有点浮夸模糊

pokemon 数据集

https://pan.baidu.com/s/1hsDn0mK#list/path=%2F

密码:4zma

如需转载请联系EasyCharts团队!

微信后台回复“转载”即可!

送福利啦!

【书籍推荐】

【必备插件】

【网易云课堂】

欢迎加入 图表绘制之魔方学院QQ群 交流探讨

返回搜狐,查看更多

责任编辑:

声明:该文观点仅代表作者本人,搜狐号系信息发布平台,搜狐仅提供信息存储空间服务。
阅读 (1514)
不感兴趣
不感兴趣
  • 广告软文
  • 重复、旧闻
  • 文章质量差
  • 文字、图片、视频等展示问题
  • 标题夸张、文不对题
  • 与事实不符
  • 低俗色情
  • 欺诈或恶意营销
  • 疑似抄袭
  • 其他问题,我要吐槽
请勿重复提交

感谢您的反馈,我们将会减少此类文章的推荐

投诉
                    </div>

                    <div class="article-oper-bord article-oper-bord">
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值