Python高级可视化库seaborn分类分析(基础整理)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cymy001/article/details/78423920

分类统计图

(1)统计柱状图barplot(均值和置信区间)
(2)灰度柱状图countplot
(3)点图pointplot(均值和置信区间)

分类散点图

当有一维数据是分类数据时,散点图成为了条带形状:
(1)航线图stripplot,设置参数添加抖动方法jitter=True(点的直接展示)
(2)生成蜂群图swarmplot,避免散点重叠(点的直接展示)

分类分布图

(1)箱式图boxplot:上下边缘、上下四分位数、中位数(近似分布)
(2.1)提琴图violinplot:箱式图 + KDE((近似分布))
(2.2)非对称提琴图:violinplot里的split=True参数

分类统计子图

(1)单分类标准的子图factorplot
(2)多分类标准的子图PairGrid


分类统计图

(1)统计柱状图barplot(均值和置信区间)

%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(style="whitegrid", color_codes=True)
np.random.seed(2017)
titanic = sns.load_dataset("titanic")

sns.barplot(x="sex", y="survived", hue="class", data=titanic)

这里写图片描述

(2)灰度柱状图countplot

%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(style="whitegrid", color_codes=True)
np.random.seed(2017)
titanic = sns.load_dataset("titanic")

sns.countplot(x="class", data=titanic)

这里写图片描述

(3)点图pointplot(均值和置信区间)

%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(style="whitegrid", color_codes=True)
np.random.seed(2017)
titanic = sns.load_dataset("titanic")

sns.pointplot(x="class", y="survived", hue="sex", data=titanic,
              palette={"male": "g", "female": "m"},
              markers=["^", "o"], linestyles=["-", "--"]) 
#palette数设置线点颜色,markers参数设置线点样式

这里写图片描述

分类散点图

当有一维数据是分类数据时,散点图成为了条带形状:
(1)航线图stripplot,设置参数添加抖动方法jitter=True(点的直接展示)

%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(style="whitegrid", color_codes=True)
np.random.seed(2017)
tips = sns.load_dataset("tips")

sns.stripplot(x="day", y="total_bill", data=tips, jitter=True)

这里写图片描述

(2)生成蜂群图swarmplot,避免散点重叠(点的直接展示)

%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(style="whitegrid", color_codes=True)
np.random.seed(2017)
tips = sns.load_dataset("tips")

sns.swarmplot(x="day", y="total_bill", hue="sex", data=tips)

这里写图片描述

分类分布图

(1)箱式图boxplot:上下边缘、上下四分位数、中位数(近似分布)

%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(style="whitegrid", color_codes=True)
np.random.seed(2017)
tips = sns.load_dataset("tips")

sns.boxplot(x="day", y="total_bill", hue="time", data=tips)

这里写图片描述

(2.1)提琴图violinplot:箱式图 + KDE((近似分布))

%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(style="whitegrid", color_codes=True)
np.random.seed(2017)
tips = sns.load_dataset("tips")

sns.violinplot(x="day", y="total_bill", hue="time", data=tips, \
               bw=2, scale="count", scale_hue=False)  #bw设置带宽,scale设置每个提琴的相对大小(count表示带宽除相应桶内频数),scale_hue设置缩放是按hue里最大宽度归一化(False),还是按单个violin归一化(True)
#可以交换x,y坐标,即横着的提琴图

这里写图片描述

(2.2)非对称提琴图:violinplot里的split=True参数

%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(style="whitegrid", color_codes=True)
np.random.seed(2017)
tips = sns.load_dataset("tips")

sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, split=True, inner="stick")  #inner参数绘制实例点

这里写图片描述

分类统计子图

(1)单分类标准的子图factorplot

%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(style="whitegrid", color_codes=True)
np.random.seed(2017)
tips = sns.load_dataset("tips")

sns.factorplot(x="day", y="total_bill", hue="smoker", col="time", data=tips, kind="swarm")

这里写图片描述

(2)多分类标准的子图PairGrid

g = sns.PairGrid(tips,
                 x_vars=["smoker", "time", "sex"],
                 y_vars=["total_bill", "tip"],
                 aspect=.75, size=3.5)
g.map(sns.violinplot, palette="pastel"); #palette参数设置颜色

这里写图片描述

展开阅读全文

没有更多推荐了,返回首页