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


Posted in Python onJune 11, 2021

在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的数据可视化使用的文章就介绍到这了,更多相关Python seaborn库countplot内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python获取Windows或Linux主机名称通用函数分享
Nov 22 Python
python任务调度实例分析
May 19 Python
Python 中 Meta Classes详解
Feb 13 Python
详解使用python crontab设置linux定时任务
Dec 08 Python
python实现图像识别功能
Jan 29 Python
python实现自动发送报警监控邮件
Jun 21 Python
详解python使用pip安装第三方库(工具包)速度慢、超时、失败的解决方案
Dec 02 Python
浅谈python输出列表元素的所有排列形式
Feb 26 Python
解决import tensorflow as tf 出错的原因
Apr 16 Python
Python如何使用ConfigParser读取配置文件
Nov 12 Python
python爬虫 requests-html的使用
Nov 30 Python
基于python制作简易版学生信息管理系统
Apr 20 Python
Python爬取某拍短视频
anaconda python3.8安装后降级
OpenCV-Python实现人脸美白算法的实例
Matplotlib可视化之添加让统计图变得简单易懂的注释
教你用Python matplotlib库制作简单的动画
PyQt5实现多张图片显示并滚动
pyqt5蒙版遮罩mask,setmask的使用
You might like
MYSQL环境变量设置方法
2007/01/15 PHP
PHP EOT定界符的使用详解
2008/09/30 PHP
PHP里的单例类写法实例
2015/06/25 PHP
PHP响应post请求上传文件的方法
2015/12/17 PHP
PHP下SSL加密解密、验证、签名方法(很简单)
2020/06/28 PHP
php实例化一个类的具体方法
2019/09/19 PHP
PHP fopen中文文件名乱码问题解决方案
2020/10/28 PHP
javascript 类方法定义还是有点区别
2009/04/15 Javascript
JavaScript组合拼接字符串的效率对比测试
2014/11/06 Javascript
Javascript基础教程之数据类型 (布尔型 Boolean)
2015/01/18 Javascript
Bootstrap开关(switch)控件学习笔记分享
2016/05/30 Javascript
js实现百度地图定位于地址逆解析,显示自己当前的地理位置
2016/12/08 Javascript
JS实现的自动打字效果示例
2017/03/10 Javascript
vue绑定数字类型 value为数字的实例
2020/08/31 Javascript
python实现同时给多个变量赋值的方法
2015/04/30 Python
pycharm远程开发项目的实现步骤
2019/01/20 Python
python Gunicorn服务器使用方法详解
2019/07/22 Python
Tensorflow限制CPU个数实例
2020/02/06 Python
ansible-playbook实现自动部署KVM及安装python3的详细教程
2020/05/11 Python
TensorFlow保存TensorBoard图像操作
2020/06/23 Python
Python基础教程之输入输出和运算符
2020/07/26 Python
利用CSS3实现文本框的清除按钮相关的一些效果
2015/06/23 HTML / CSS
详解css3中的伪类before和after常见用法
2020/11/17 HTML / CSS
La Redoute英国官网:法国时尚品牌
2017/04/27 全球购物
描述内存分配方式以及它们的区别
2016/10/15 面试题
土木工程应届生求职信
2013/10/31 职场文书
男女朋友协议书
2014/04/23 职场文书
小学标准化建设汇报材料
2014/08/16 职场文书
2014最新离职证明范本
2014/09/12 职场文书
2014年“四风”问题个人整改措施
2014/09/17 职场文书
领导班子在批评与自我批评座谈会上的发言
2014/09/28 职场文书
安全生产月标语
2014/10/07 职场文书
幼儿园教师节感谢信
2015/01/23 职场文书
质量整改通知单
2015/04/21 职场文书
2016年学校禁毒宣传活动工作总结
2016/04/05 职场文书
css常用字体属性与背景属性介绍
2022/02/28 HTML / CSS