Python可视化 | Seaborn02

barplot和countplot

具体用法如下:

seaborn.barplot(x=Noney=Nonehue=Nonedata=Noneorder=Nonehue_order=None,ci=95n_boot=1000units=Noneorient=Nonecolor=Nonepalette=Nonesaturation=0.75errcolor='.26'errwidth=Nonecapsize=Noneax=Noneestimator=<function mean>,**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.

estimator : callable that maps vector -> scalar, optional

#设置对每类变量的计算函数,默认为平均值,可修改为max、median、max等

Statistical function to estimate within each categorical bin.

ax : matplotlib Axes, optional #设置子图位置,将在下节介绍绘图基础

Axes object to draw the plot onto, otherwise uses the current Axes.

orient : “v” | “h”, optional #控制绘图的方向,水平或者竖直

Orientation of the plot (vertical or horizontal).

capsize : float, optional #设置误差棒帽条的宽度

Width of the “caps” on error bars.
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import median
sns.set_style("whitegrid")
tips = sns.load_dataset("tips") #载入自带数据集
#x轴为分类变量day,y轴为数值变量total_bill,利用颜色再对sex分类
ax = sns.barplot(x="day", y="total_bill", hue="sex", data=tips)
# ax = sns.barplot(x="day", y="tip", data=tips, estimator=median)
#设置中位数为计算函数,注意y轴已显示为median
plt.show()

import seaborn as sns
import matplotlib.pyplot as plt
from numpy import median
sns.set_style("whitegrid")
tips = sns.load_dataset("tips") #载入自带数据集
#x轴为分类变量day,y轴为数值变量total_bill,利用颜色再对sex分类
# ax = sns.barplot(x="day", y="total_bill", hue="sex", data=tips)
ax = sns.barplot(x="day", y="tip", data=tips, estimator=median)
#设置中位数为计算函数,注意y轴已显示为median
plt.show()

 

 

countplot计数图

seaborn.countplot - seaborn 0.7.1 documentation

countplot故名思意,计数图,可将它认为一种应用到分类变量的直方图,也可认为它是用以比较类别间计数差,调用count函数的barplot。

seaborn.countplot(x=Noney=Nonehue=Nonedata=Noneorder=Nonehue_order=Noneorient=Nonecolor=Nonepalette=Nonesaturation=0.75ax=None**kwargs)

x, y, hue : names of variables in data or vector data, optional

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.

orient : “v” | “h”, optional #设置水平或者垂直显示

Orientation of the plot (vertical or horizontal).

ax : matplotlib Axes, optional #设置子图位置,将在下节介绍绘图基础

Axes object to draw the plot onto, otherwise uses the current Axes.
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="darkgrid")
titanic = sns.load_dataset("titanic") #titanic经典数据集,带有登船人员的信息
#源数据集class代表三等舱位,who代表人员分类,男女小孩,对每一类人数计数
ax = sns.countplot(x="class", hue="who", data=titanic)
plt.show()

 

 

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set(style="white") #设置绘图背景

# Load the example planets dataset
planets = sns.load_dataset("planets")

# Make a range of years to show categories with no observations
years = np.arange(2000, 2015) #生成2000-2015连续整数

# Draw a count plot to show the number of planets discovered each year
#选择调色板,绘图参数与顺序,factorplot一种分类变量的集合作图方式,利用kind选择bar、plot等绘图参数以后将具体介绍
g = sns.factorplot(x="year", data=planets, kind="count",
                   palette="BuPu", size=6, aspect=1.5, order=years)
g.set_xticklabels(step=2) #设置x轴标签间距为2年

plt.show()

# -*-coding:utf-8 -*-
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
dc=pd.read_csv('F:\硬件开发工具\wenjian/dc.csv')

#第一个图,我们来探索下英雄与坏蛋们眼睛颜色的分布
#看看坏蛋的眼睛颜色是不是都是什么奇怪的颜色
sns.countplot(y='EYE',data=dc,hue='ALIGN')
plt.show()

# -*-coding:utf-8 -*-
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
dc=pd.read_csv('F:\硬件开发工具\wenjian/dc.csv')

#第一个图,我们来探索下英雄与坏蛋们眼睛颜色的分布
#看看坏蛋的眼睛颜色是不是都是什么奇怪的颜色
# sns.countplot(y='EYE',data=dc,hue='ALIGN')
#第二个图我们来用用barplot,发现每个角色的出场次数是个数值型变量,是个不错的探索数据,我们来看看各个阵营间与性别间的平均上场次数
sns.barplot(y='APPEARANCES',x='ALIGN',data=dc,hue='SEX')
plt.show()

# -*-coding:utf-8 -*-
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
dc=pd.read_csv('F:\硬件开发工具\wenjian/dc.csv')

#第一个图,我们来探索下英雄与坏蛋们眼睛颜色的分布
#看看坏蛋的眼睛颜色是不是都是什么奇怪的颜色
# sns.countplot(y='EYE',data=dc,hue='ALIGN')
#第二个图我们来用用barplot,发现每个角色的出场次数是个数值型变量,是个不错的探索数据,我们来看看各个阵营间与性别间的平均上场次数
# sns.barplot(y='APPEARANCES',x='ALIGN',data=dc,hue='SEX')
#再来复习一波上一节的知识,来看看好坏阵营间登场小于50次的各大配角们的登场次数分布
ax1=sns.kdeplot(dc['APPEARANCES'][dc['ALIGN']=='Good Characters'][dc['APPEARANCES']<50],shade=True,label='good')

ax2=sns.kdeplot(dc['APPEARANCES'][dc['ALIGN']=='Bad Characters'][dc['APPEARANCES']<50],shade=True,label='bad')
plt.show()

 

参考地址:https://zhuanlan.zhihu.com/p/24553277

https://zhuanlan.zhihu.com/p/34007570

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值