Python中seaborn库之countplot的数据可视化使用
作者:cymx66688 发布时间:2023-08-10 20:38:56
在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])
来源:https://www.cnblogs.com/cymx66688/p/10536403.html
猜你喜欢
- 最近在这找了好久的js菜单,都没找到满意的,今天找了个,觉得不错,最重要的是简单,希望大家可以参照一下先看看效果图吧:代码特点:js+css
- Web设计师可以使用HTML4和CSS2.1完成一些很酷的东西。我们可以在不使用陈旧的基于table布局的基础上完成文档逻辑结构并创建内容丰
- 由于存在函数内部不能访问全局作用的,所以就需要一种可以引入上一级作用域的语法结构,可以通过use使用函数声明时所在作用域的变量的值。php的
- 在查看MySQL对文件的操作中,它在不同的操作系统上对文件的操作,除了使用标准C运行库函数,包括open、close、seek等,在Win3
- 安装 SQL2000 时,系统经常会提示:操作被挂起,要求重新启动计算机,如图1: 图1重新启动后,再次安装时问题仍然存在。解决办
- 今天一个域名查询系统出现故障,该系统是用的ASP调用XMLHTTP获取whois库的数据,具体错误如下: msxml3.dll 错
- 现在很多地方都需要用到关键词过滤功能。比如一般的服务器都不允许一些词出现在网页上,站长有时候会对在本网站发布信息的内容进行一个广告过滤等。雨
- Introduction简介So what is POSH? No, it's not just some new clothing
- 从业这几年,自己写过的和帮人参谋的所谓“设计规范”不少了,这个东西大概在中国的决策层眼里是这么回事儿 - 一帮农民在一块田里种粮食,起先天气
- 在良好的数据库设计基础上,能有效地使用索引是SQL Server取得高性能的基础,SQL Server采用基于代价的优化模型,它对每一个提交
- 因为正则不够完善,所以代码中不能直接出现 <? 和 ?>如果是字符串,可以拆开写 "<" + &quo
- 如何显示一个文本文件?完整显示文本文件的代码如下: Write(STRING) WriteLine(STRING) WriteBlan
- slice 可以用来获取数组片段,它返回新数组,不会修改原数组。除了正常用法,slice 经常用来将 array-like 对象转换为 tr
- 本期薯片会的目的是讨论人物角色在设计中如何应用的问题。围绕了以下几个问题开展了讨论:1、 角色能干什么?2、 包含哪
- 在学会了SELECT语句的构造和用途之后你就该学习如何使用它了。在你所掌握的数据库工具下,这可能意味着你得按下某个写着“执行”字样的按钮。在
- 前言Go的错误处理这块是日常被大家吐槽较多的地方,我在工作中也观察到一些现象,比较严重的是在各层级的逻辑代码中对错误的处理有些重复。比如,有
- 一、什么是用户体验?用户体验的名词解释用户体验(User Experience,简称UE)是一种纯主观的在用户使用一个产品(服务)的过程中建
- 学习目的 掌握如何用ADO.NET插入新的记录 我们学得好快,今天做一个简易的新闻发布网页,可以说是个演示型的,只是让大家能理插入数据的最主
- 一段时间以来,发现有很多人XHTML都不会用,不光是普通的初学者,有的程序员都不是很清楚该怎么写这个XHTML,我这里呢算是把一些常见的应用
- Dreamweaver MX 2004的强大功能以及更加完善的人性化设置已经深受大家喜爱。在此笔者就谈