/ Python And R Data science skills: 92 regression plots lmplot

Sunday, 18 February 2018

92 regression plots lmplot

92 regrision plots lmplot

Regression Plots

Seaborn has many built-in capabilities for regression plots, however we won't really discuss regression until the machine learning section of the course, so we will only cover the lmplot() function for now.

lmplot allows you to display linear models, but it also conveniently allows you to split up those plots based off of features, as well as coloring the hue based off of features.

Let's explore how this works:

In [1]:
import seaborn as sns
%matplotlib inline
In [2]:
tips = sns.load_dataset('tips')
In [23]:
sns.lmplot(x='total_bill',y='tip',data=tips)
Out[23]:
<seaborn.axisgrid.FacetGrid at 0x139e2048>
In [24]:
sns.lmplot(x='total_bill',y='tip',data=tips,hue='sex')
Out[24]:
<seaborn.axisgrid.FacetGrid at 0x139eed68>

Working with Markers

lmplot kwargs get passed through to regplot which is a more general form of lmplot(). regplot has a scatter_kws parameter that gets passed to plt.scatter. So you want to set the s parameter in that dictionary, which corresponds (a bit confusingly) to the squared markersize. In other words you end up passing a dictionary with the base matplotlib arguments, in this case, s for size of a scatter plot. In general, you probably won't remember this off the top of your head, but instead reference the documentation.

In [25]:
# http://matplotlib.org/api/markers_api.html
sns.lmplot(x='total_bill',y='tip',data=tips,hue='sex',palette='coolwarm',
           markers=['o','v'])
Out[25]:
<seaborn.axisgrid.FacetGrid at 0x13a54400>
In [26]:
# http://matplotlib.org/api/markers_api.html
sns.lmplot(x='total_bill',y='tip',data=tips,hue='sex',
           markers=['o','v'],scatter_kws={'s':100})
Out[26]:
<seaborn.axisgrid.FacetGrid at 0x13b196d8>

Using a Grid

We can add more variable separation through columns and rows with the use of a grid. Just indicate this with the col or row arguments:

In [27]:
sns.lmplot(x='total_bill',y='tip',data=tips,col='sex')
Out[27]:
<seaborn.axisgrid.FacetGrid at 0x13af87f0>
In [11]:
sns.lmplot(x="total_bill", y="tip", row="sex", col="time",data=tips)
Out[11]:
<seaborn.axisgrid.FacetGrid at 0xc316c18>
In [15]:
sns.lmplot(x='total_bill',y='tip',data=tips,col='day',hue='sex')
Out[15]:
<seaborn.axisgrid.FacetGrid at 0xe58ce10>
In [19]:
sns.lmplot(x='total_bill',y='tip',data=tips,col='day',hue='sex',
          aspect=0.1,size=8)
Out[19]:
<seaborn.axisgrid.FacetGrid at 0x11467588>
In [20]:
sns.lmplot(x='total_bill',y='tip',data=tips,col='day',hue='sex',
          aspect=0.6,size=8)
Out[20]:
<seaborn.axisgrid.FacetGrid at 0x11569630>
In [22]:
sns.lmplot(x='total_bill',y='tip',data=tips,col='day',hue='sex',
          aspect=0.6,size=4)
Out[22]:
<seaborn.axisgrid.FacetGrid at 0x1382b048>

No comments:

Post a Comment