草庐IT

20种方法Seaborn绘制箱型图

皮皮大 2023-03-28 原文

公众号:尤而小屋
作者:Peter
编辑:Peter

大家好,我是Peter~

本文介绍的是如何使用 seaborn 的 boxplot 方法来绘制箱型图,先看看部分图形的绘制效果:

[图片上传失败...(image-ce2350-1649776295600)]

参数

绘制图形中的主要参数如下:

[图片上传失败...(image-dea971-1649776295601)]

更多资料可参考官网地址:https://seaborn.pydata.org/generated/seaborn.boxplot.html

箱型图

箱型图是一种用作显示一组数据分散情况资料的统计图,它能够快速显示数据中的异常值情况,其形状像盒子,因而得名,也称之为盒须图、盒式图、盒装图或者箱型图

1977年,美国著名数学家John W. Tukey首先在他的著作《Exploratory Data Analysis》中介绍了箱形图。

[图片上传失败...(image-efaead-1649776295601)]

四分位数是箱型图中最为重要的概念。Q3和Q1的差距称为四分位距(InterQuartile Range, IQR):IQR=Q3-Q1

[图片上传失败...(image-7d8fdc-1649776295601)]

内置数据

Seaborn也有自己内置的数据集:

import seaborn as sns
# style设置
sns.set_theme(style="whitegrid")  

tips

消费数据集tips

[图片上传失败...(image-2d5045-1649776295601)]

iris

知名的鸢尾花数据集

[图片上传失败...(image-5d37c-1649776295601)]

水平箱型图

In [4]:

# 方式1:指定x为某个Series型数据

ax = sns.boxplot(x=tips["total_bill"])
# 方式2:传入x和data参数
ax = sns.boxplot(x="total_bill",
                data=tips)

[图片上传失败...(image-b49157-1649776295601)]

垂直箱型图

In [6]:

ax = sns.boxplot(y=tips["total_bill"])

# 方式2:传入y和data参数
# ax = sns.boxplot(y="total_bill", data=tips)

[图片上传失败...(image-4b6be9-1649776295601)]

参数orient

In [7]:

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

[图片上传失败...(image-abcb54-1649776295601)]

改变x-y的位置:

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

[图片上传失败...(image-5d1161-1649776295601)]

参数order

对指定的参数进行排序

In [11]:

# 默认情况
ax = sns.boxplot(
    x="sex",
    y="tip", 
    data=tips
)

[图片上传失败...(image-7286f1-1649776295601)]

下面的例子中我们引入了参数order,主要是查看x轴中两个标签;

In [12]:

ax = sns.boxplot(
    x="sex",
    y="tip", 
    data=tips,
    order=["Female","Male"]  # 引入参数
)

和默认情况下的排序不同,按照指定的顺序进行展示:

[图片上传失败...(image-8447a3-1649776295601)]

参数hue使用

参数hue主要是用来进行色条的调节

In [13]:

ax = sns.boxplot(
    x="day",
    y="tip", 
    data=tips,
    hue="sex"  # 引入参数
)

[图片上传失败...(image-361e01-1649776295601)]

参数hue_order

In [14]:

ax = sns.boxplot(
    x="day",
    y="tip", 
    data=tips,
    hue="sex",
    hue_order=["Female","Male"]  # 引入参数
)

[图片上传失败...(image-8b614a-1649776295601)]

参数palette

颜色版的设置使用palette

In [15]:

ax = sns.boxplot(
    x="day",
    y="tip", 
    data=tips,
    hue="sex",
    palette="Set3"  # 颜色版
)

[图片上传失败...(image-f3b7be-1649776295601)]

ax = sns.boxplot(
    x="day",
    y="tip", 
    data=tips,
    hue="sex",
    palette="Set2"  # 颜色版
)

[图片上传失败...(image-fb070b-1649776295601)]

[图片上传失败...(image-f58187-1649776295601)]

[图片上传失败...(image-10da2e-1649776295601)]

大小参数

主要是saturation、width、fliersize、linewidth、whis的设置

In [19]:

# 全部是默认情况
ax = sns.boxplot(x="sex",y="tip", data=tips, hue="day")

[图片上传失败...(image-8dd956-1649776295601)]

ax = sns.boxplot(
    x="sex",
    y="tip", 
    data=tips,
    hue="day",
    width=0.7,
    linewidth=3,
)

[图片上传失败...(image-3d33a7-1649776295601)]

ax = sns.boxplot(
    x="sex",
    y="tip", 
    data=tips,
    hue="day",
    width=0.7,
    linewidth=3,
    whis=3  # 引入whis
)

[图片上传失败...(image-64cbb-1649776295601)]

参数notch

自定义缺口

In [22]:

ax = sns.boxplot(
    x="day", 
     y="total_bill", 
     hue="sex",
     data=tips,
    notch=True   # 加入参数
)

[图片上传失败...(image-e768b1-1649776295601)]

参数dodge

必须和hue一起使用,控制同一个分组下面的箱型图是分开绘制还是重叠在一起

In [23]:

ax = sns.boxplot(
    x="day", 
  y="total_bill", 
  hue="sex",
  data=tips, 
  dodge=False)

[图片上传失败...(image-43fe91-1649776295601)]

ax = sns.boxplot(
    x="day", 
    y="total_bill",
    hue="sex",
    data=tips, 
    dodge=True)

[图片上传失败...(image-a07b12-1649776295601)]

catplot-分类图

箱型图和分类图的结合使用

In [26]:

ax = sns.catplot(
    x="sex", 
    y="total_bill",
    hue="smoker", 
    col="time",
    data=tips, 
    kind="box",  # 箱型图
    height=4, 
    aspect=.7)

[图片上传失败...(image-8b17b2-1649776295601)]

ax = sns.catplot(
    x="total_bill",
    y="sex",            
    hue="smoker", 
    col="time",
    data=tips, 
    orient="h",  # 水平方向
    kind="box",  # 箱型图
    height=4, 
    aspect=.7,
    palette="Set2"
)

[图片上传失败...(image-407a70-1649776295601)]

ax = sns.catplot(
    x="sex", 
    y="total_bill",
    hue="smoker", 
    col="time",
    data=tips, 
    kind="violin",  # 小提琴图
    height=4, 
    aspect=.7)

[图片上传失败...(image-d895c4-1649776295601)]

有关20种方法Seaborn绘制箱型图的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  5. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  6. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  7. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  8. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  9. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  10. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

随机推荐