seaborn seaborn回归分析绘图(四)
#%%
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
# np.random.seed(sum(map(ord, "regression")))
tips = sns.load_dataset("tips") # 加载内置的数据集,得到pandas的DataFram的格式
tips.to_csv("tips.csv",index=False) # 将数据保存到本地为csv文件
tips.head()
#%%
gqr_tip=pd.read_csv("tips.csv")
gqr_tip
#%% md
## 使用seaborn绘制回归关系
#%% md
regplot()和lmplot()都可以绘制回归关系,推荐regplot()
#%%
sns.regplot(x="total_bill", y="tip", data=tips)
#%%
sns.lmplot(x="total_bill", y="tip", data=tips);
#%%
sns.regplot(data=tips,x="size",y="tip")
#%%
sns.regplot(x="size", y="tip", data=tips, x_jitter=.05) # x_jitter:给数据添加上一个小的抖动
#%%
anscombe = sns.load_dataset("anscombe")
anscombe
#%%
sns.regplot(x="x", y="y", data=anscombe.query("dataset == 'I'"),
ci=None, scatter_kws={"s": 100}) #ci=None可以不显示阴影。
#%%
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
ci=None, scatter_kws={"s": 80})
#%%
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
order=2, ci=None, scatter_kws={"s": 80}); # ci 置信区间 浮点数或None
#%%
sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips); # 设置hue参数,可以为不同类别的点绘制不同的颜色
#%%
sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips,
markers=["o", "x"], palette="Set1");
#%%
sns.lmplot(x="total_bill", y="tip", hue="smoker", col="time", data=tips); # 设置col参数,可以将数据绘制不同的散点图中
# row,col 更多分类变量进行平铺显示 变量名
#%%
sns.lmplot(x="total_bill", y="tip", hue="smoker",
col="time", row="sex", data=tips); # row,col 更多分类变量进行平铺显示 变量名
#%%
f, ax = plt.subplots(figsize=(5, 5))
sns.regplot(x="total_bill", y="tip", data=tips, ax=ax);
#%% md
col_wrap:“Wrap” the column variable at this width, so that the column facets span multiple rows
size :Height (in inches) of each facet
#%%
sns.lmplot(x="total_bill", y="tip", col="day", data=tips,
col_wrap=2, size=4);
#%%
sns.lmplot(x="total_bill", y="tip", col="day", data=tips,
aspect=.8);
#%%