10 分钟 python seaborn 绘图入门 (Ⅲ): Boxplot 与 Violinplot

浏览: 1882

image.png

箱线图——百度百科

"盒式图" 或叫 "盒须图" "箱形图",,其绘制须使用常用的统计量,能提供有关数据位置和分散情况的关键信息,尤其在比较不同的母体数据时更可表现其差异。

如上图所示,标示了图中每条线表示的含义,其中应用到了分位值(数)的概念。

主要包含五个数据节点,将一组数据从大到小排列,分别计算出他的上边缘,上四分位数,中位数,下四分位数,下边缘。

具体用法如下:seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, fliersize=5, linewidth=None, whis=1.5, notch=False, ax=None, **kwargs)

Parameters:

x, y, hue : names of variables in data or vector data, optional #设置 x,y 以及颜色控制的变量

Inputs for plotting long-form data. See examples for interpretation.

data : DataFrame, array, or list of arrays, optional #设置输入的数据集

Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form. 

order, hue_order : lists of strings, optional #控制变量绘图的顺序

Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.


import seaborn as sns
sns.set_style("whitegrid")
tips
= sns.load_dataset("tips") #载入自带数据集#研究三个变量之间的关系,是否抽烟与日期为分类变量,消费是连续变量
#结论发现吸烟者在周末消费明显大于不吸烟的人
ax = sns.boxplot(x="day", y="total_bill", hue="smoker",data=tips, palette="Set3")


image.png

Senior Example Ⅰ for Practice

boxplot 探索 Pokemon 宠物小精灵的属性分布。

属性科普

至最新的精灵宝可梦游戏第七世代 “精灵宝可梦太阳 / 月亮”,总共 18 种属性,分别是:一般、火、水、草、电、冰、虫、飞行、地面、岩石、格斗、超能力、幽灵、毒、恶、钢、龙、妖精。不仅精灵宝可梦(台译神奇宝贝,港译宠物小精灵)有属性,招式也有其属性,而其威力则以乘数计。攻击优势属性时,可以到双倍甚至四倍计算,攻击劣势属性时,攻击威力减半甚至无效。

单一属性的计算如图,而双属性计算方法是 2 个属性受到的加成比相乘,例如:使用火系技能攻击岩石系和钢系会造成 1 倍伤害(2X0.5=1);使用火系技能攻击水系和龙系双属性的敌人会造成 1/4 的伤害(0.5X0.5=0.25);使用火系技能攻击虫系和草系双属性的敌人会造成 4 倍伤害(2X2=4);使用格斗系的技能攻击鬼系敌人无效 (2X0=0)。

image.png

特性和技能的特殊效果会影响伤害计算,例如:使用技能冷冻干燥会对水属性造成双倍伤害;嗅觉可以使普通系技能命中鬼属性,并造成 1 倍伤害


# -*-coding:utf-8 -*-
import pandas as pd
import seaborn as sns
pokemon=pd.read_csv('H:/zhihu/Pokemon.csv',)
#每列分别是名称,第一属性,第二属性,总数,血量,攻击,防御,特殊攻击,特殊防御,速度,代数,传奇
pokemon.head()


image.png


#观察各属性的分布
pkmn = pokemon.drop(['Total', '#','Generation','Legendary'],1);
sns
.boxplot(data=pkmn);


HP 值普遍较低,但是 HP 大于 120 肉盾异常值倒是挺多

image.png

#牛刀小试,第一属性与HP值/speed速度分布关系
sns.boxplot(y='Type 1',x='HP',data=pokemon)
sns
.boxplot(y='Type 1',x='Speed',data=pokemon)

image.png

FLYING 飞行类速度一骑绝尘

image.png

Violinplot 琴形图

Violinplot 结合了箱线图与核密度估计图的特点,它表征了在一个或多个分类变量情况下,连续变量数据的分布并进行了比较,它是一种观察多个数据分布有效方法。

image.png

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, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)

split : bool, optional #琴形图是否从中间分开两部分

When using hue nesting with a variable that takes two levels, setting split to True will draw half of a violin for each level. This can make it easier to directly compare the distributions.

scale : {“area”, “count”, “width”}, optional #用于调整琴形图的宽带。area——每个琴图拥有相同的面域;count——根据样本数量来调节宽度;width——每个琴图则拥有相同的宽度。

The method used to scale the width of each violin. If area, each violin will have the same area. If count, the width of the violins will be scaled by the number of observations in that bin. If width, each violin will have the same width.

inner : {“box”, “quartile”, “point”, “stick”, None}, optional #控制琴图内部数据点的形态。box——绘制微型 boxplot;quartiles——绘制四分位的分布;point/stick——绘制点或小竖条。

Representation of the datapoints in the violin interior. If box, draw a miniature boxplot. If quartiles, draw the quartiles of the distribution. If point or stick, show each underlying datapoint. Using None will draw unadorned violins.

Senior Example Ⅱ for Practice


#以速度为y轴,世代为x轴区分"传奇",来绘制攻击能力的分布图
#由于传奇系很稀少,scale选择width,保持两边宽度相同,inder选择stick加入分布竖条
sns.violinplot(y='Attack',x='Generation',data=pokemon,hue='Legendary',palette="Set3",split=True,scale="width", inner="stick", sc


image.png

世代并没有影响攻击能力的差异,传奇系的生物倒是攻击能力超高!!虽然我也不懂什么 Legendary,但是看起来确实很厉害的样子


sns.set_style("whitegrid") #调整背景为白底
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 6)) #由于变量过多,调整下图表大小
#继续暗中观察,攻击与防御分布如何
ax1 = sns.violinplot(x="Type 1", y="Attack",
                   data=pokemon,
                   scale="width", palette="Set3")
plt.figure(figsize=(12, 6))
ax = sns.violinplot(x="Type 1", y="Defense",
                   data=pokemon,
                   scale="width", palette="Set3")


image.png

Dragon 龙类生物攻击十分强势,出来吧喷火龙

image.png

image.png

image.png

Steel 钢属性生物则是肉盾型,皮糙肉厚,著名代表大钢蛇

好奇观察一波皮卡丘的属性与电属性生物的情况


pokemon[pokemon['Name']=='Pikachu']
sns
.boxplot(data=pkmn[pkmn['Type 1']=='Electric'])


image.png

欢迎加入  图表绘制之魔方学院QQ群  交流探讨


推荐 0
本文由 EasyCharts 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册