当前位置:网站首页>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"])
tipsOut[12]:

In [13]:
ax = sns.barplot(x="day",
y="tip",
hue="weekend",
data=tips,
dodge=False)
- END -边栏推荐
- Cs231n notes (medium) -- applicable to 0 Foundation
- 国泰君安网上开户安全吗
- 英特尔第13代Raptor Lake处理器信息曝光:更多核心 更大缓存
- SQL injection sqllabs (basic challenges) 11-20
- Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
- Memo 00
- list使用Stream流进行根据元素某属性数量相加
- Query the latest record in SQL
- 10 minutes to help you get ZABBIX monitoring platform alarm pushed to nail group
- ES6深入—async 函数 与 Symbol 类型
猜你喜欢
随机推荐
verilog实现计算最大公约数和最小公倍数
20. [stm32] realize the function of intelligent garbage can by using ultrasonic module and steering gear
Relationship between objects and classes
Arduino控制微小的六足3D打印机器人
求解汉诺塔问题【修改版】
Li Kou today's question -729 My schedule I
给自己打打气
Why should we learn mathematical modeling?
The database of the server is not connected to 200310060 "unknown error" [the service is up, the firewall is off, the port is on, and the netlent port is not connected]
服务器的数据库连不上了2003,10060“Unknown error“【服务已起、防火墙已关、端口已开、netlent 端口不通】
程序员如何提升自己的格局?
【毕业季】作为一名大二计科在校生,我有话想说
CISP-PTE之SQL注入(二次注入的应用)
通过的英特尔Evo 3.0整机认证到底有多难?忆联科技告诉你
ES6深入—ES6 Class 类
Intelligent metal detector based on openharmony
Memo 00
Appium automation test foundation - appium basic operation API (II)
Noi / 1.3 01: a+b problem
Noi / 1.4 07: collect bottle caps to win awards







![21.[STM32]I2C协议弄不懂,深挖时序图带你编写底层驱动](/img/f4/2c935dd9933f5cd4324c29c41ab221.png)

