当前位置:网站首页>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 -
边栏推荐
猜你喜欢
Batch update in the project
开发中Boolean类型使用遇到的坑
Research and practice of super-resolution technology in the field of real-time audio and video
抽象类中子类与父类
Codasip adds verify safe startup function to risc-v processor series
Information collection of penetration test
vulnhub-Root_ this_ box
Appium automation test foundation - appium basic operation API (I)
服务器的数据库连不上了2003,10060“Unknown error“【服务已起、防火墙已关、端口已开、netlent 端口不通】
16.[STM32]从原理开始带你了解DS18B20温度传感器-四位数码管显示温度
随机推荐
Transaction rollback exception
vant tabbar遮挡内容的解决方式
The visual experience has been comprehensively upgraded, and Howell group and Intel Evo 3.0 have jointly accelerated the reform of the PC industry
Defining strict standards, Intel Evo 3.0 is accelerating the upgrading of the PC industry
10 minutes to help you get ZABBIX monitoring platform alarm pushed to nail group
18.[stm32] read the ROM of DS18B20 temperature sensor and realize multi-point temperature measurement
21. [STM32] I don't understand the I2C protocol. Dig deep into the sequence diagram to help you write the underlying driver
The OBD deployment mode of oceanbase Community Edition is installed locally
Apiccloud cloud debugging solution
Data communication foundation - Ethernet port mirroring and link aggregation
漫画:什么是八皇后问题?
给自己打打气
Cs231n notes (medium) -- applicable to 0 Foundation
Query the latest record in SQL
18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度
17.[STM32]仅用三根线带你驱动LCD1602液晶
Li Kou today's question -729 My schedule I
漫画:什么是分布式事务?
List de duplication and count the number
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!