Python数据可视化-seaborn库之countplot

在Python数据可视化中,seaborn较好的提供了图形的一些可视化功效。

seaborn官方文档见链接:http://seaborn.pydata.org/api.html

countplot是seaborn库中分类图的一种,作用是使用条形显示每个分箱器中的观察计数。接下来,对seaborn中的countplot方法进行详细的一个讲解,希望可以帮助到刚入门的同行。

  1. 导入seaborn库
1 import seaborn as sns
  1. 使用countplot
1 sns.countplot()

countplot方法中必须要x或者y参数,不然就报错。

官方给出的countplot方法及参数:

sns.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)

 

 下面讲解countplot方法中的每一个参数。

x, y, hue : names of variables in ``data`` or vector data, optional. Inputs for plotting long-form data. See examples for interpretation. 
 
  • 第一种方式

x: x轴上的条形图,以x标签划分统计个数

y: y轴上的条形图,以y标签划分统计个数

hue: 在x或y标签划分的同时,再以hue标签划分统计个数

example:

1 sns.set(style="darkgrid")
2 titanic = sns.load_dataset("titanic")
3 ax = sns.countplot(x="class", data=titanic)

1 sns.set(style="darkgrid")
2 titanic = sns.load_dataset("titanic")
3 ax = sns.countplot(y="class", data=titanic)

1 sns.countplot(x="class", hue="who", data=titanic)

  • 第二种方法

x: x轴上的条形图,直接为series数据

y: y轴上的条形图,直接为series数据

1 sns.set(style="darkgrid")
2 titanic = sns.load_dataset("titanic")
3 sns.countplot(x=titanic['class'])

1 sns.set(style="darkgrid")
2 titanic = sns.load_dataset("titanic")
3 sns.countplot(y=titanic['class'])

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.

data: DataFrame或array或array列表,用于绘图的数据集,x或y缺失时,data参数为数据集,同时x或y不可缺少,必须要有其中一个。

1 sns.countplot(x='class', data=titanic)

palette:使用不同的调色板

1 sns.countplot(x="who", data=titanic, palette="Set3")

猜你喜欢

转载自www.cnblogs.com/cymx66688/p/10536403.html
今日推荐