有时我们想在子图中绘制多个条形图(barplot),这个例子展示了一个很好的方法。
上传样本数据
import seaborn as sns
sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
tips.head(2)
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
按日期和性别聚合数据,并汇总每个维度下的小费(tips)
tips_agg = tips.groupby(['day','sex'])['tip'].sum().reset_index()
tips_agg
day | sex | tip | |
---|---|---|---|
0 | Thur | Male | 89.41 |
1 | Thur | Female | 82.42 |
2 | Fri | Male | 26.93 |
3 | Fri | Female | 25.03 |
4 | Sat | Male | 181.95 |
5 | Sat | Female | 78.45 |
6 | Sun | Male | 186.78 |
7 | Sun | Female | 60.61 |
如果未安装,请安装最新的 seaborn 包
!pip install seaborn==0.11.2
可视化数据中每个不同工作日的男性与女性的小费数量
from matplotlib import pyplot as plt
import seaborn as sns
import math
# automatically adjust the rows and colums
all_days = list(set(tips_agg['day']))
n_cols = 3
n_rows = math.ceil(len(all_days)/n_cols)
# give the figsize
fig, axes = plt.subplots( n_rows, n_cols, figsize=(20, 10))
for i in range(n_rows):
for j in range(n_cols):
index = i*n_cols+j
if index >= len(all_days):
break
day = all_days[index]
bp = sns.barplot(ax=axes[i, j], data=tips_agg[tips_agg['day']==day], x='sex', y='tip')
bp.set(title=day)
for item in bp.get_xticklabels():
item.set_rotation(45)
# if the x axis title is very long, this configuration will be very useful
plt.tight_layout()
更多精彩内容,请点击
data science examples