Study:day16-数据可视化进阶之Seaborn模块
1.1 Seaborn的基本使用
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import sys
import seaborn as sns
print('Python version:', sys.version)
print('Pandas version:', pd.__version__)
print('Numpy version:', np.__version__)
print('Matplotlib version:', matplotlib.__version__)
Python version: 3.12.4 | packaged by Anaconda, Inc. | (main, Jun 18 2024, 15:03:56) [MSC v.1929 64 bit (AMD64)]
Pandas version: 2.2.2
Numpy version: 1.26.4
Matplotlib version: 3.8.4
tips = pd.read_csv("./data/tips.csv")
tips.head()
|
消费金额 ($) |
小费金额 ($) |
客人性别 |
是否吸烟 |
周几 |
就餐时间 |
一起就餐人数 (个) |
0 |
16.99 |
1.01 |
Female |
No |
Sun |
Dinner |
2 |
1 |
10.34 |
1.66 |
Male |
No |
Sun |
Dinner |
3 |
2 |
21.01 |
3.50 |
Male |
No |
Sun |
Dinner |
3 |
3 |
23.68 |
3.31 |
Male |
No |
Sun |
Dinner |
2 |
4 |
24.59 |
3.61 |
Female |
No |
Sun |
Dinner |
4 |
plt.rcParams['font.sans-serif'] = ['STsong']
sns.relplot(
data=tips,
x="小费金额 ($)",
y="消费金额 ($)",
col="就餐时间",
hue="是否吸烟",
style="客人性别",
size=100,
)
plt.savefig("./image/seaborn_01.png")

1.2 seaborn 中的两类绘图函数
- figure-level(图形级别的):类似于在matplotlib中,通过plt.figure()创建一个包含多个子图的对象,他们管理一个整体的图形对象
- axes-level(坐标轴级别的):可以针对某个特定的子图进行高度的定制化,类似于在matplotlib中使用ax对象进行绘图,可以精确地控制图形的各个细节
1.2.1 figure-level(图形级别的)
penguins = pd.read_csv("./data/penguins.csv")
plt.rcParams['font.sans-serif'] = ['STsong']
sns.displot(
data=penguins,
x="喙长 (毫米)",
hue="企鹅的种类",
col="企鹅的种类",
palette=["red", "blue", "green"],
binwidth=1,
height=5,
aspect=1,
)
<seaborn.axisgrid.FacetGrid at 0x295b00c3dd0>

penguins
|
企鹅的种类 |
岛屿 |
喙长 (毫米) |
喙深 (毫米) |
鳍长 (毫米) |
体重 |
性别 |
0 |
Adelie |
Torgersen |
39.1 |
18.7 |
181.0 |
3750.0 |
MALE |
1 |
Adelie |
Torgersen |
39.5 |
17.4 |
186.0 |
3800.0 |
FEMALE |
2 |
Adelie |
Torgersen |
40.3 |
18.0 |
195.0 |
3250.0 |
FEMALE |
3 |
Adelie |
Torgersen |
NaN |
NaN |
NaN |
NaN |
NaN |
4 |
Adelie |
Torgersen |
36.7 |
19.3 |
193.0 |
3450.0 |
FEMALE |
... |
... |
... |
... |
... |
... |
... |
... |
339 |
Gentoo |
Biscoe |
NaN |
NaN |
NaN |
NaN |
NaN |
340 |
Gentoo |
Biscoe |
46.8 |
14.3 |
215.0 |
4850.0 |
FEMALE |
341 |
Gentoo |
Biscoe |
50.4 |
15.7 |
222.0 |
5750.0 |
MALE |
342 |
Gentoo |
Biscoe |
45.2 |
14.8 |
212.0 |
5200.0 |
FEMALE |
343 |
Gentoo |
Biscoe |
49.9 |
16.1 |
213.0 |
5400.0 |
MALE |
344 rows × 7 columns
1.2.2 axes-level多子图个性化
fig, axs = plt.subplots(1, 2, figsize=(12, 4), dpi=100,
gridspec_kw=dict(width_ratios=[4, 2])
)
sns.scatterplot(
data=penguins,
x="喙深 (毫米)",
y="喙长 (毫米)",
hue="企鹅的种类",
ax=axs[0]
)
sns.histplot(
data=penguins,
x="企鹅的种类",
hue="企鹅的种类",
ax=axs[1],
shrink=.5,
)
<Axes: xlabel='企鹅的种类', ylabel='Count'>

1.3 相关关系图-relational plots
tips = pd.read_csv("./data/tips.csv")
sns.relplot(
data=tips,
x="消费金额 ($)",
y="小费金额 ($)",
color="red",
hue="客人性别",
kind="scatter",
col="是否吸烟",
col_order=["Yes", "No"],
row="就餐时间",
row_order=["Lunch", "Dinner"],
size="一起就餐人数 (个)",
sizes=(1, 300),
palette=["red", "blue"],
)
<seaborn.axisgrid.FacetGrid at 0x295b1c3e3f0>

1.4 直方图
- displot(kind=‘hist’): figure-level
- hisplot: axes-level
sns.displot(
data=penguins,
x="企鹅的种类",
kind="hist",
bins=10,
discrete=True,
binwidth=