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

 更新时间:2021年06月11日 10:24:09   作者:cymx66688  
在Python数据可视化中,seaborn较好的提供了图形的一些可视化功效。本文详细的介绍了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()

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)

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

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

第二种方法

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

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

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

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

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

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'])

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

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

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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Python装饰器的四种定义形式

    详解Python装饰器的四种定义形式

    装饰器(decorator)在Python框架中扮演着重要角色,是Python中实现切面编程(AOP)的重要手段,这篇文章主要介绍了Python装饰器的四种定义形式,需要的朋友可以参考下
    2022-11-11
  • python多版本工具miniconda的配置优化实现

    python多版本工具miniconda的配置优化实现

    通过Miniconda,您可以轻松地创建和管理多个Python环境,同时确保每个环境具有所需的依赖项和软件包,本文主要介绍了python多版本工具miniconda的配置优化实现,感兴趣的可以了解一下
    2024-01-01
  • Python运算符的使用保姆级教学

    Python运算符的使用保姆级教学

    这篇文章主要给大家介绍了关于Python运算符使用的相关资料,文中总结了Python中的算术运算符、赋值运算符、比较运算符、逻辑运算符、位运算符和成员运算符的用法和特性,需要的朋友可以参考下
    2024-11-11
  • 使用Python实现计算DICOM图像两点真实距离

    使用Python实现计算DICOM图像两点真实距离

    这篇文章主要为大家详细介绍了如何使用Python实现计算DICOM图像两点真实距离,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-11-11
  • 用Python编写简单的定时器的方法

    用Python编写简单的定时器的方法

    这篇文章主要介绍了用Python编写简单的定时器的方法,主要用到了Python中的threading模块,需要的朋友可以参考下
    2015-05-05
  • 如何用python处理excel表格

    如何用python处理excel表格

    在本篇文章里小编给大家整理了关于python处理excel表格的详细步骤内容,需要的朋友们可以参考下。
    2020-06-06
  • Python 专题四 文件基础知识

    Python 专题四 文件基础知识

    本文主要讲述了Python文件基础知识,包括文件的打开、读写、关闭操作、使用循环读写文件及迭代器的知识。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • django框架如何集成celery进行开发

    django框架如何集成celery进行开发

    本文给大家详细讲解了在django框架中如何集成celery进行开发,步骤非常详细,有需要的小伙伴可以参考下
    2017-05-05
  • caffe binaryproto 与 npy相互转换的实例讲解

    caffe binaryproto 与 npy相互转换的实例讲解

    今天小编就为大家分享一篇caffe binaryproto 与 npy相互转换的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Pycharm报错Non-zero exit code (2)的完美解决方案

    Pycharm报错Non-zero exit code (2)的完美解决方案

    最近在使用pycharm安装或升级模块时出现了错误,下面这篇文章主要给大家介绍了关于Pycharm报错Non-zero exit code (2)的完美解决方案,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-06-06

最新评论