Python中seaborn库之countplot数据可视化的使用方法

Python中seaborn库之countplot数据可视化的使用方法

这篇文章给大家分享的是有关Python中seaborn库之countplot数据可视化的使用方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

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

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

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

导入seaborn库

import seaborn as sns

使用countplot

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方法中的每一个参数。以泰坦尼克号为例。

原始数据如下:

sns.set(style='darkgrid')
titanic = sns.load_dataset('titanic')
titanic.head()

Python中seaborn库之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标签划分统计个数

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

Python中seaborn库之countplot数据可视化的使用方法

sns.countplot(y="class", data=titanic)

Python中seaborn库之countplot数据可视化的使用方法

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

Python中seaborn库之countplot数据可视化的使用方法

第二种方法

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

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

sns.countplot(x=titanic['class'])

Python中seaborn库之countplot数据可视化的使用方法

sns.countplot(y=titanic['class'])

Python中seaborn库之countplot数据可视化的使用方法

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不可缺少,必须要有其中一个。

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

Python中seaborn库之countplot数据可视化的使用方法

order, hue_order : lists of strings, optional.Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.
order, hue_order分别是对x或y的字段排序,hue的字段排序。排序的方式为列表。

sns.countplot(x='class', data=titanic, order=['Third', 'Second', 'First'])

Python中seaborn库之countplot数据可视化的使用方法

sns.countplot(x='class', hue='who', data=titanic, hue_order=['woman', 'man', 'child'])

Python中seaborn库之countplot数据可视化的使用方法

orient : "v" | "h", optional
Orientation of the plot (vertical or horizontal). This is usually
inferred from the dtype of the input variables, but can be used to
specify when the "categorical" variable is a numeric or when plotting
wide-form data.
强制定向,v:竖直方向;h:水平方向,具体实例未知。

color : matplotlib color, optional
Color for all of the elements, or seed for a gradient palette.

palette : palette name, list, or dict, optional.Colors to use for the different levels of the ``hue`` variable.
Should be something that can be interpreted by :func:`color_palette`, or a dictionary mapping hue levels to matplotlib colors.

palette:使用不同的调色板

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

Python中seaborn库之countplot数据可视化的使用方法

ax : matplotlib Axes, optional
Axes object to draw the plot onto, otherwise uses the current Axes.

ax用来指定坐标系。

fig, ax = plt.subplots(1, 2, figsize=(10, 5))
sns.countplot(x='class', data=titanic, ax=ax[0])
sns.countplot(y='class', data=titanic, ax=ax[1])

Python中seaborn库之countplot数据可视化的使用方法

感谢各位的阅读!关于“Python中seaborn库之countplot数据可视化的使用方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!