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

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

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

  1. 导入seaborn库
 import seaborn as sns
  1. 使用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'])

1 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的更多相关文章

  1. Python数据可视化-seaborn

    详细介绍可以看seaborn官方API和example galler. 1  set_style( )  set( ) set_style( )是用来设置主题的,Seaborn有五个预设好的主题: d ...

  2. Python数据可视化的四种简易方法

    摘要: 本文讲述了热图.二维密度图.蜘蛛图.树形图这四种Python数据可视化方法. 数据可视化是任何数据科学或机器学习项目的一个重要组成部分.人们常常会从探索数据分析(EDA)开始,来深入了解数据, ...

  3. 推荐一款Python数据可视化神器

    1. 前言 在日常工作中,为了更直观的发现数据中隐藏的规律,察觉到变量之间的互动关系,人们常常借助可视化帮助我们更好的给他人解释现象,做到一图胜千文的说明效果. 在Python中,常见的数据可视化库有 ...

  4. Python数据可视化基础讲解

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:爱数据学习社 首先,要知道我们用哪些库来画图? matplotlib ...

  5. Python数据可视化编程实战——导入数据

    1.从csv文件导入数据 原理:with语句打开文件并绑定到对象f.不必担心在操作完资源后去关闭数据文件,with的上下文管理器会帮助处理.然后,csv.reader()方法返回reader对象,通过 ...

  6. Python数据可视化——使用Matplotlib创建散点图

    Python数据可视化——使用Matplotlib创建散点图 2017-12-27 作者:淡水化合物 Matplotlib简述: Matplotlib是一个用于创建出高质量图表的桌面绘图包(主要是2D ...

  7. Python数据可视化编程实战pdf

    Python数据可视化编程实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1vAvKwCry4P4QeofW-RqZ_A 提取码:9pcd 复制这段内容后打开百度 ...

  8. 【数据科学】Python数据可视化概述

    注:很早之前就打算专门写一篇与Python数据可视化相关的博客,对一些基本概念和常用技巧做一个小结.今天终于有时间来完成这个计划了! 0. Python中常用的可视化工具 Python在数据科学中的地 ...

  9. python --数据可视化(一)

    python --数据可视化 一.python -- pyecharts库的使用 pyecharts--> 生成Echarts图标的类库 1.安装: pip install pyecharts ...

随机推荐

  1. 存储类&作用域&生命周期&链接属性

    链接属性 (1)大家知道程序从源代码到最终可执行程序,经历的过程:编译.链接. (2)编译阶段就是把源代码搞成.o目标文件,目标文件里面有很多符号和代码段.数据段.bss段等分段.符号就是编程中的变量 ...

  2. 在线SQL

    有时候为了简单测试一下SQL语法,自己机器上没有安装,可使用在线的SQL环境. 我找到了两个: https://rextester.com/l/sql_server_online_compiler ( ...

  3. Apache Flink教程

    1.Apache Flink 教程 http://mp.weixin.qq.com/mp/homepage?__biz=MzIxMTE0ODU5NQ==&hid=5&sn=ff5718 ...

  4. 怎么用MATLAB产生FPGA所需的hamming窗系数

    需求 在FPGA处理中如果需要对待处理数据加窗,则需要窗系数存储在ROM中以供使用. 前言 MATLAB窗函数说明 流程 比如加个hamming窗,8192点,16bit放大,最终系数18bit位宽. ...

  5. MT【328】向量里的最佳逼近

    已知平面向量$\overrightarrow {a},\overrightarrow {b}$满足$|\overrightarrow {a}|=4,|\overrightarrow {b}|=2$.若 ...

  6. 【redis】redis5.0的一些新特性

    redis5.0总共增加了12项新特性,如下: 1.新增加的Stream(流)数据类型,这样redis就有了6大数据类型,另外五种是String(字符串),Hash(哈希),List(列表),Set( ...

  7. dgraph实现基本操作

    dgraph实现基本操作 简单介绍 dgraph 是一个分布式图数据库 mutate 为一个突变, 一般认为添加数据或者是删除数据为一个突变 query 为一个查询 golang实现dgraph的基本 ...

  8. 大数据-hadoop生态之-HDFS

    一.HDFS初识 hdfs的概念: HDFS,它是一个文件系统,用于存储文件,通过目录树定位文件,其次,他是分布式的,由很多服务器联合起来 实现功能,集群中的服务器各有各自的角色 HDFS设计适合一次 ...

  9. IDEA 常用配置

    调节代码字体大小 设置:File --- Settings... --- Editor --- General --- 勾选Change font size(Zoom) with Ctrl + Mou ...

  10. python3 两层dict字典转置

    python3; 两层字典 dict =(type, dict2) dict2 = (k_value, index) dictss = { 10: {3: 1, 4: 2, 5: 3, 6: 4, 7 ...