10分钟python图表绘制 | seaborn入门(四):回归模型lmplot
Seaborn介绍
官方链接:Seaborn: statistical data visualization
Seaborn是一种基于matplotlib的图形可视化python libraty。它提供了一种高度交互式界面,便于用户能够做出各种有吸引力的统计图表。
Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图,而使用matplotlib就能制作具有更多特色的图。应该把Seaborn视为matplotlib的补充,而不是替代物。同时它能高度兼容numpy与pandas数据结构以及scipy与statsmodels等统计模式。掌握seaborn能很大程度帮助我们更高效的观察数据与图表,并且更加深入了解它们。
:回归模型lmplot/v2-14b93a648dc3f3eb9890e586104299ca_b.png)
安装seaborn
- 利用pip安装
pip install seaborn
2. 在Anaconda环境下,打开prompt
conda install seaborn lmplot
seaborn.lmplot - seaborn 0.7.1 documentation
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
import seaborn as sns
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"):回归模型lmplot/v2-31cabf2c3e67d4f8b5ddee83252f0cdc_b.png)
#研究在不同星期下,消费总额与消费的回归关系,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)
:回归模型lmplot/v2-bc31bbc57630aaccef4ced951344c6f9_b.png)
#继续研究pokemon数据集
import pandas as pd
import seaborn as sns
pokemon=pd.read_csv('H:/zhihu/Pokemon.csv')
pokemon.head():回归模型lmplot/v2-6c91ae571abcfa024f497319a0ce2c27_b.png)
:回归模型lmplot/v2-c944df48380ad78e91c10128374318bb_b.jpg)
#观察每一代攻击与防御的分布,利用二次多项式逼近
sns.lmplot(x="Defense", y="Attack",data=pokemon,col="Generation", hue="Generation",col_wrap=3, size=3,order=2):回归模型lmplot/v2-370d59f769fcda660b493ac625aca6f1_b.png)
#继续在同一图中观察不同代的sp.Atk,Sp.Def线性关系
sns.lmplot(x="Sp. Atk", y="Sp. Def", data=pokemon, hue='Generation', size=5,order=1):回归模型lmplot/v2-d4beea4d08e4568068fa66f12355de06_b.png)
sp.Atk,Sp.Def线性相关性不高导致图像有点浮夸模糊
更多关于python数据分析与挖掘内容请关注我的专栏:数与码
或者关注我的知乎账号:知行
才疏学浅,希望观众老爷们多提意见,谢谢
专栏持续更新中,求赞求关注