python绘制月亮_10分钟python图表绘制 | seaborn入门(三):Boxplot与Violinplot

Seaborn介绍

Seaborn是一种基于matplotlib的图形可视化python libraty。它提供了一种高度交互式界面,便于用户能够做出各种有吸引力的统计图表。

Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图,而使用matplotlib就能制作具有更多特色的图。应该把Seaborn视为matplotlib的补充,而不是替代物。同时它能高度兼容numpy与pandas数据结构以及scipy与statsmodels等统计模式。掌握seaborn能很大程度帮助我们更高效的观察数据与图表,并且更加深入了解它们。

其有如下特点:基于matplotlib aesthetics绘图风格,增加了一些绘图模式

增加调色板功能,利用色彩丰富的图像揭示您数据中的模式

运用数据子集绘制与比较单变量和双变量分布的功能

运用聚类算法可视化矩阵数据

灵活运用处理时间序列数据

利用网格建立复杂图像集

安装seaborn利用pip安装

pip install seaborn

2. 在Anaconda环境下,打开prompt

conda install seaborn

boxplot箱线图

箱线图——百度百科

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

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

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

具体用法如下:

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")

Senior Example Ⅰ for Practice

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

属性科普

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

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

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

# -*-coding:utf-8 -*-

import pandas as pd

import seaborn as sns

pokemon=pd.read_csv('H:/zhihu/Pokemon.csv',)

#每列分别是名称,第一属性,第二属性,总数,血量,攻击,防御,特殊攻击,特殊防御,速度,代数,传奇

pokemon.head()

#观察各属性的分布

pkmn = pokemon.drop(['Total', '#','Generation','Legendary'],1);

sns.boxplot(data=pkmn);

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

#牛刀小试,第一属性与HP值/speed速度分布关系

sns.boxplot(y='Type 1',x='HP',data=pokemon)

sns.boxplot(y='Type 1',x='Speed',data=pokemon)FLYING飞行类速度一骑绝尘

Violinplot琴形图

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

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", scale_hue=False)

世代并没有影响攻击能力的差异,传奇系的生物倒是攻击能力超高!!虽然我也不懂什么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")

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

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

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

pokemon[pokemon['Name']=='Pikachu']

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

皮卡丘这渣属性在同类中还达不到平均水平 ,为什么 = =

持续更新中,求赞求关注~谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值