📅  最后修改于: 2023-12-03 14:52:15.926000             🧑  作者: Mango
在可视化数据时,除了展示数据的分布和频次,我们还可能需要在图表中加入百分比等信息,以更直观地展示数据。在 seaborn 库中,可以使用 countplot 函数展示数据的频次分布,并可以利用 matplotlib.pyplot 库中的 text 函数来添加百分比信息,下面我们详细介绍如何在 countplot 中添加百分比的方法。
import seaborn as sns
import matplotlib.pyplot as plt
我们使用 seaborn 库中的示例数据集 tips。
tips = sns.load_dataset('tips')
我们使用 countplot 绘制性别与付款方式的频率条形图。
ax = sns.countplot(x='sex', hue='day', data=tips)
我们先计算性别和付款方式的频率,并封装在字典中。
totals = tips.groupby(['sex', 'day']).size()
totals_dict = {'sex': [], 'day': [], 'total': []}
for (sex, day), amount in totals.iteritems():
totals_dict['sex'].append(sex)
totals_dict['day'].append(day)
totals_dict['total'].append(amount)
totals = pd.DataFrame(totals_dict)
proportions = []
for sex in ['Male', 'Female']:
for day in ['Thur', 'Fri', 'Sat', 'Sun']:
subdf = totals[(totals['sex']==sex) & (totals['day']==day)]
if len(subdf):
total = subdf.iloc[0]['total']
proportions.append(total)
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
for i, rect in enumerate(ax.patches):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width() / 2,
height + 4,
'{:.2%}'.format(height / proportions[i]),
ha='center', va='bottom',
fontsize=12, color=colors[i%4])
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# 加载数据集
tips = sns.load_dataset('tips')
# 绘制 countplot
ax = sns.countplot(x='sex', hue='day', data=tips)
# 计算每种类别的数量
totals = tips.groupby(['sex', 'day']).size()
totals_dict = {'sex': [], 'day': [], 'total': []}
for (sex, day), amount in totals.iteritems():
totals_dict['sex'].append(sex)
totals_dict['day'].append(day)
totals_dict['total'].append(amount)
totals = pd.DataFrame(totals_dict)
proportions = []
for sex in ['Male', 'Female']:
for day in ['Thur', 'Fri', 'Sat', 'Sun']:
subdf = totals[(totals['sex']==sex) & (totals['day']==day)]
if len(subdf):
total = subdf.iloc[0]['total']
proportions.append(total)
# 添加百分比标签
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
for i, rect in enumerate(ax.patches):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width() / 2,
height + 4,
'{:.2%}'.format(height / proportions[i]),
ha='center', va='bottom',
fontsize=12, color=colors[i%4])
plt.show()
以上就是在 countplot 中添加百分比的方法,通过计算总数和每个小分组的数量,我们可以轻松地将百分比信息添加到图表中。