Python seaborn线型回归曲线
qq_21478261 人气:0本文速览
1、绘图数据准备
依旧使用鸢尾花iris数据集,详细介绍见之前文章。
#导入本帖要用到的库,声明如下: import matplotlib.pyplot as plt import numpy as np import pandas as pd import palettable from pandas import Series,DataFrame from sklearn import datasets import seaborn as sns import palettable #导入鸢尾花iris数据集(方法一) #该方法更有助于理解数据集 iris=datasets.load_iris() x, y =iris.data,iris.target y_1 = np.array(['setosa' if i==0 else 'versicolor' if i==1 else 'virginica' for i in y]) pd_iris = pd.DataFrame(np.hstack((x, y_1.reshape(150,1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class']) #astype修改pd_iris中数据类型object为float64 pd_iris['sepal length(cm)']=pd_iris['sepal length(cm)'].astype('float64') pd_iris['sepal width(cm)']=pd_iris['sepal width(cm)'].astype('float64') pd_iris['petal length(cm)']=pd_iris['petal length(cm)'].astype('float64') pd_iris['petal width(cm)']=pd_iris['petal width(cm)'].astype('float64') #导入鸢尾花iris数据集(方法二) #该方法有时候会卡巴斯基,所以弃而不用 #import seaborn as sns #iris_sns = sns.load_dataset("iris")
数据集简单查看
2、seaborn.regplot
seaborn.regplot(x, y, data=None, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, seed=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=True, dropna=True, x_jitter=None, y_jitter=None, label=None, color=None, marker='o', scatter_kws=None, line_kws=None, ax=None)
regplot默认参数线型回归图
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2)#设置主题,文本大小 g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, color='#000000',#设置marker及线的颜色 marker='*',#设置marker形状 )
分别设置点和拟合线属性
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, color='#000000', marker='*', scatter_kws={'s': 60,'color':'g',},#设置散点属性,参考plt.scatter line_kws={'linestyle':'--','color':'r'}#设置线属性,参考 plt.plot
置信区间(confidence interval)设置
注意拟合线周围阴影面积变化
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, color='#000000', marker='*', ci=60,#置信区间设置,默认为95%置信区间,越大线周围阴影部分面积越大 )
拟合线延伸与坐标轴相交
# extend the regression line to the axis limits plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, color='#000000', marker='*', truncate=False,#让拟合线与轴相交 )
拟合离散变量曲线
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) x_discrete=[0 if i=='setosa' else 1 if i=='versicolor' else 2 for i in pd_iris['class']]# g=sns.regplot(x=x_discrete, y='sepal width(cm)', data=pd_iris,#x此时为离散变量 color='#000000', marker='*', )
多项式回归( polynomial regression)拟合曲线
plt.figure(dpi=110) sns.set(style="whitegrid",font_scale=1.2) g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, marker='*', order=4,#默认为1,越大越弯曲 scatter_kws={'s': 60,'color':'#016392',},#设置散点属性,参考plt.scatter line_kws={'linestyle':'--','color':'#c72e29'}#设置线属性,参考 plt.plot )
3、seaborn.lmplot
seaborn.lmplot(x, y, data, hue=None, col=None, row=None, palette=None, col_wrap=None, height=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, seed=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=True, x_jitter=None, y_jitter=None, scatter_kws=None, line_kws=None, size=None)
seaborn.lmplot结合seaborn.regplot()和FacetGrid,比seaborn.regplot()更灵活,可绘制更个性化的图形。
按变量分类拟合回归线
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, hue='class', ) g.fig.set_size_inches(10,8)
散点marker设置
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, hue='class', markers=['+','^','o'], #设置散点marker ) g.fig.set_size_inches(10,8)
散点调色盘
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, hue='class', markers=['+','^','*'], scatter_kws={'s':180}, palette=["#01a2d9", "#31A354", "#c72e29"],#调色盘 ) g.fig.set_size_inches(10,8)
拟合线属性设置
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, hue='class', markers=['+','^','*'], scatter_kws={'s':180}, line_kws={'linestyle':'--'},#拟合线属性设置 palette=["#01a2d9", "#31A354", "#c72e29"], ) g.fig.set_size_inches(10,8)
绘制分面图
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, col='class',#按class绘制分面图 markers='*', scatter_kws={'s':150,'color':'#01a2d9'}, line_kws={'linestyle':'--','color':'#c72e29'},#直线属性设置 ) g.fig.set_size_inches(10,8)
加载全部内容