barplot参数 python_Python可视化21|Seaborn.catplot(上)小提琴图等四类图

"pythonic生物人"的第66篇分享3c783b0ad1a95828cf5a4f1b9d46d20b.png50bfcc4bb7ae5a35a447d412184d967e.png50bfcc4bb7ae5a35a447d412184d967e.png

本文介绍seaborn.catplot函数可视化分类数据,涉及如下8种图,更换父类catplot中的kind参数即可,也可使用对应的子函数如下:

  • stripplot(),此时(kind="strip",默认);
  • swarmplot(),此时(kind="swarm")
  • boxplot(),此时(kind="box");
  • violinplot(),此时(kind="violin");
  • boxenplot(),此时(kind="boxen")
  • pointplot(),此时(kind="point");
  • barplot(),此时(kind="bar");
  • countplot(),此时(kind="count")

每类图函数都有自己独特的参数,本文主要介绍前四类图


本文将了解什么?

1、seaborn.catplot简介2、绘图数据集准备3、seaborn.stripplot(分类散点图) stripplot不分类散点图 stripplot分类散点图 设置点的属性  多重分类  catplot()结合stripplot和FacetGrid分图显示  4、seaborn.swarmplot(成簇散点图) swarmplot不分类散点图 设置点的属性   5、seaborn.boxplot(箱图或箱线图)6、seaborn.violinplot(小提琴图) 不分类小提琴图 分类小提琴图  inner参数  多重分类小提琴图  多重分类左右显示小提琴图

正文开始啦

1、seaborn.catplot简介

实用场景:categorical data, one of the main variables is “categorical” (divided into discrete groups)
语法:seaborn.catplot(x=None, y=None, hue=None, data=None, row=None, col=None, col_wrap=None, estimator=, ci=95, n_boot=1000, units=None, seed=None, order=None, hue_order=None, row_order=None, col_order=None, kind='strip', height=5, aspect=1, orient=None, color=None, palette=None, legend=True, legend_out=True, sharex=True, sharey=True, margin_titles=False, facet_kws=None, **kwargs)
seaborn.catplot可分3大类,更细分8小类:
分类散点图, stripplot(),此时(kind="strip",默认);swarmplot(),此时(kind="swarm")
分类分布图, boxplot(),此时(kind="box");violinplot(),此时(kind="violin");boxenplot(),此时(kind="boxen")
分类估计图, pointplot(),此时(kind="point");barplot(),此时(kind="bar");countplot(),此时(kind="count")

8类图长什么样子?(以下绘图使用鸢尾花数据集)

for i in  list("point, bar, strip, swarm, box, violin, boxen".split(', ')):
    g=sns.catplot(x='sepal length(cm)', y='class', data=pd_iris, kind='%s'%i,
                  palette='husl',
                 )
    plt.title("kind='%s'"%i)
    g.fig.set_size_inches(8,6)#设置图形大小
g=sns.catplot(x='sepal length(cm)', hue='class', data=pd_iris, kind='count',
              palette='husl',
             )
plt.title("kind='count'")
g.fig.set_size_inches(12,8)

e036a0c53f31e4f738bc46588253fd18.png48e6e2f592848021bc5ca32923f6742c.png87685b13eefaeb7f426853c94689e0b1.png31a4eadbc7ad4fcb2cdd9f0e3edc9214.pngcc7bd4b093ae46437c7fad8de4b3bbf6.png6684da40f4454ef7ff3fb3d9e6e3790d.pnge36bf525a41b7e6f5dddfd04f7acc59c.png


2、绘图数据集准备

还是使用鸢尾花iris数据集:Python可视化|matplotlib10-绘制散点图scatter

#导入本帖要用到的库,声明如下:import matplotlib.pyplot as pltimport numpy as npimport pandas as pd import palettablefrom pandas import Series,DataFramefrom sklearn import datasetsimport seaborn as snsimport 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")

数据集查看f128f5bd4b02b573b69f9cdd6096ccb7.png

3、seaborn.stripplot(分类散点图)

使用场景:两个变量,有一个是分类变量
语法:seaborn.stripplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, jitter=True, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)

  • stripplot不分类散点图

#stripplot不分类散点图
plt.figure(figsize=(8,5))
sns.set(style="whitegrid")
sns.stripplot(x='sepal length(cm)',data=pd_iris,#传入数据pd_iris第一列
              palette='husl',
             )
d4463f5525acc11e90ec424c486b2359.png
  • stripplot分类散点图

#stripplot分类散点图
plt.figure(dpi=70)
sns.set(style="whitegrid")
sns.stripplot(y='sepal length(cm)',x='class',data=pd_iris,#传入数据pd_iris第一列及最后一列
              palette='husl',
             )
e90753b797cae6bcd004e440468a39a6.png
  • 设置点的属性

#设置点的属性
plt.figure(dpi=70)
sns.set(style="whitegrid")
sns.stripplot(y='sepal length(cm)',x='class',data=pd_iris,
              palette='Set1', #marker='*',#也可如下修改marker
              **dict(marker='*',s=15,alpha=0.7),#设置点形状、大小、透明度等,更多见matplotlib.axes.Axes.scatter
             )
377b2ce09eb7c1dc037c108333d17f85.png
  • 多重分类

#给数据加一列花期(上、下旬)
flowering=pd.Series(['early' if i>4.0 and i<5.0 else 'middle' for i in pd_iris['sepal length(cm)']])
pd_iris1=pd.concat([pd_iris,flowering],axis=1)#拼接,默认按行拼接,即axis=0
pd_iris1.rename(columns={0:'flowering'}, inplace = True)#替换列名称
pd_iris1.head()
8e02c150090969056ce0676aad829b7c.png
#class类下再按照flowering分类
plt.figure(dpi=70)
sns.set(style="whitegrid")
sns.stripplot(y='sepal length(cm)',x='class',hue='flowering',data=pd_iris1,#hue='flowering'添加子类
              palette='Set1',
              **dict(marker='^',s=7)              
             )
365e2aaf5241b11ee80b12689b6494be.png
  • catplot()结合stripplot和FacetGrid分图显示

##使用catplot()结合 barplot()和FacetGrid分图显示
plt.figure(figsize=(12,5))
sns.set(style="whitegrid")
g=sns.catplot(y='sepal length(cm)',x='class',col='flowering',data=pd_iris1,#col='flowering'
              palette='Set1',
              **dict(marker='^',s=7)             
             )
g.fig.set_size_inches(12,6)
8b9bc7dbcd1f1de79ba890f279100b1d.png

4、seaborn.swarmplot(成簇散点图)

使用场景:与stripplot()类似,只是swarmplot中数据点经过了成簇处理【the points are adjusted (only along the categorical axis) so that they don’t overlap.】,是数据点不重叠的stripplot()
语法:seaborn.swarmplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)参数和stripplot()一样,简单介绍下。

  • swarmplot不分类散点图

#swarmplot不分类散点图
plt.figure(dpi=70)
sns.set(style="whitegrid")
sns.swarmplot(x='sepal length(cm)',data=pd_iris,#传入数据pd_iris第一列
              palette='husl',
             )
6ec897c1cf60e8a3a0a82c120466f202.png
  • 设置点的属性

#设置点的属性
plt.figure(dpi=70)
sns.set(style="whitegrid")
sns.swarmplot(y='sepal length(cm)',x='class',data=pd_iris,
              palette='Set1', #marker='*',#也可如下修改marker
              **dict(marker='*',s=15,alpha=0.7),#设置点形状、大小、透明度等,更多见matplotlib.axes.Axes.scatter
             )
8195ad1827d9cc915fad5647b43977c1.png

5、seaborn.boxplot**(箱图或箱线图)**

详细见Python可视化17seaborn-箱图boxplot


6、seaborn.violinplot(小提琴图)

更多可参考:Python可视化17seaborn-箱图boxplot语法:seaborn.violinplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, dodge=True, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)
以下介绍部分特异参数。

  • 不分类小提琴图

#sepal length(cm)不分类小提琴图
plt.figure(dpi=70)
sns.set(style="whitegrid")
sns.violinplot(y='sepal length(cm)',data=pd_iris,#传入数据pd_iris第一列
              palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,
             )
a37fb87df845cc0712520787e6ca9706.png
  • 分类小提琴图

#按class分类小提琴图
plt.figure(dpi=70)
sns.set(style="whitegrid")
sns.violinplot(y='sepal length(cm)',x='class',data=pd_iris1,
              palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors, 
             )
83750c578d9ad438c0a3d7f9d297d785.png
  • inner参数

箱子中间图形显示模式

#中间显示模式inner参数for i in list('box,quartile,point,stick'.split(',')):
    plt.figure(dpi=70)
    sns.set(style="whitegrid")
    sns.violinplot(y='sepal length(cm)',x='class',data=pd_iris1,
                   palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,
                   inner='%s'%i,#可选{“box”(默认), “quartile”, “point”, “stick”, None}
                  )
    plt.title("inner='%s'"%i)
plt.figure(dpi=70)
sns.set(style="whitegrid")
sns.violinplot(y='sepal length(cm)',x='class',data=pd_iris1,
               palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,
               inner=None,
              )
plt.title("inner=None")

5613336637d7ac64ea8eef80faebf6d9.png8d67a7d002b676ce780288daa3fe5b09.pnga012082bb616d44202d6132550e63db9.png28208f3e9f5ec20b1c9a8a5362e5799e.png6fa4a0e79df260737a248ade45aaefd1.png

  • 多重分类小提琴图

#按class分类后再按flowering分类小提琴图
plt.figure(dpi=70)
sns.set(style="whitegrid")
sns.violinplot(y='sepal length(cm)',x='class',hue='flowering',data=pd_iris1,
              palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors, 
             )
a0efc5d533158302220f828d8b462682.png
  • 多重分类左右显示小提琴图

#按class分类后再按flowering分类左右显示小提琴图
plt.figure(figsize=(8,5))
sns.set(style="whitegrid")
sns.violinplot(y='sepal length(cm)',x='class',hue='flowering',data=pd_iris1,
              palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,
               split=True#左右显示flowering分类
             )
913c1a64b584e9afb77261ead7f600fa.png

参考资料

  • http://seaborn.pydata.org/generated/seaborn.catplot.html#seaborn.catplot
同系列文章
Python可视化17seborn-箱图boxplotPython可视化20|Seaborn散点图&&折线图

3c783b0ad1a95828cf5a4f1b9d46d20b.png3c783b0ad1a95828cf5a4f1b9d46d20b.png原创不易"点赞"、"在看"765a56f99dcdb8428ce5bfe13d6ac8f9.png励下呗3d8a29ff36bf4176072c5f61cb122e22.png31dadf1656da3741e06b73d62366a5db.png

bc4ad62c59cacc8a07c48e9c4452c3f9.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值