当前位置:网站首页>Seaborn绘制11个柱状图
Seaborn绘制11个柱状图
2022-07-05 15:43:00 【俊红的数据分析之路】
本文介绍的是如何使用seaborn来绘制各种柱状图
基础柱状图
水平柱状图
标题设置
基于DataFrame绘图
hue参数设置
颜色处理
多维度处理
个人很喜欢的一个Seaborn绘制的图形:

导入库
Seaborn是matplotlib的高级封装,所以matplotlib还是要同时导入:
In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_theme(style="whitegrid")
sns.set_style('darkgrid')
导入内置数据
使用的是seaborn中内置的一份消费tips数据集:
In [2]:
tips = sns.load_dataset("tips")
tips.head()

基础柱状图
In [3]:
x = ["A","B","C"]
y = [1, 2, 3]
sns.barplot(x, y)
plt.show()

绘制水平柱状图:
# 水平柱状图
x = ["A","B","C"]
y = [1, 2, 3]
sns.barplot(y, x)
plt.show()

设置标题
In [14]:
x = ["A","B","C"]
y = [1, 2, 3]
fig = sns.barplot(x, y)
fig.set_title('title of seaborn')
plt.show()

指定x-y-data
In [5]:
# 通过DataFrame来指定
ax = sns.barplot(x="day", y="tip", data=tips)
plt.show()

hue参数
实现的分组显示数据
In [6]:
ax = sns.barplot(x="day",
y="total_bill",
hue="sex",
data=tips)

水平柱状图
In [7]:
ax = sns.barplot(x="total_bill",
y="day",
data=tips)

自定义顺序
In [8]:
ax = sns.barplot(x="total_bill",
y="day",
# 添加order参数,指定顺序
order=["Sat","Fri","Sun","Thur"], # 自定义
data=tips)

颜色处理
使用一种颜色
In [9]:
ax = sns.barplot(x="size",
y="total_bill",
data=tips,
color="salmon",
saturation=.5)

颜色渐变
In [10]:
ax = sns.barplot(x="size",
y="tip",
data=tips,
palette="Blues")

多维分组
In [11]:
g = sns.catplot(x="sex",
y="total_bill",
hue="smoker",
col="time",
data=tips,
kind="bar",
height=4,
aspect=.7)

True/False分组
In [12]:
tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
tips
Out[12]:

In [13]:
ax = sns.barplot(x="day",
y="tip",
hue="weekend",
data=tips,
dodge=False)

- END -
边栏推荐
- 数据湖(十四):Spark与Iceberg整合查询操作
- 利用GrayLog告警功能实现钉钉群机器人定时工作提醒
- 一些认知的思考
- ES6深入—ES6 Generator 函数
- Research and development efficiency measurement index composition and efficiency measurement methodology
- List uses stream flow to add according to the number of certain attributes of the element
- 10分钟帮你搞定Zabbix监控平台告警推送到钉钉群
- Interval DP (gravel consolidation)
- 记一次'非常诡异'的云安全组规则问题排查过程
- 迁移/home分区
猜你喜欢
Arduino controls a tiny hexapod 3D printing robot
Data communication foundation NAT network address translation
sql中set标签的使用
具有倍数关系的时钟切换
The visual experience has been comprehensively upgraded, and Howell group and Intel Evo 3.0 have jointly accelerated the reform of the PC industry
Subclasses and superclasses of abstract classes
19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)
开发中Boolean类型使用遇到的坑
OceanBase社区版之OBD方式部署方式本地安装
写单元测试的时候犯的错
随机推荐
Six common transaction solutions, you sing, I come on stage (no best, only better)
Find the root of the following equation by chord cutting method, f (x) =x^3-5x^2+16x-80=0
Why should we learn mathematical modeling?
利用GrayLog告警功能实现钉钉群机器人定时工作提醒
Arduino controls a tiny hexapod 3D printing robot
Noi / 1.5 06: element maximum span value of integer sequence
Convert obj set to entity set
vulnhub-Root_ this_ box
Quick completion guide for manipulator (IX): forward kinematics analysis
19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)
Analytic hierarchy process of mathematical modeling (including Matlab code)
Cs231n notes (top) - applicable to 0 Foundation
迁移/home分区
Mongodb getting started Tutorial Part 04 mongodb client
10 minutes to help you get ZABBIX monitoring platform alarm pushed to nail group
16.[STM32]从原理开始带你了解DS18B20温度传感器-四位数码管显示温度
This article takes you through the addition, deletion, modification and query of JS processing tree structure data
Replknet: it's not that large convolution is bad, but that convolution is not large enough. 31x31 convolution. Let's have a look at | CVPR 2022
异常com.alibaba.fastjson.JSONException: not match : - =
国泰君安网上开户安全吗