在 Python 的条形图中包含百分比细分

发布于 2025-01-09 13:39:46 字数 1830 浏览 4 评论 0原文

是否可以在 Python 的条形图中包含百分比细分?

这是我的代码,我想按收入水平过滤单词的频率

'''

Q1 = data.iloc[:, 5:14]

SQ1 =Q1.groupby(data['Income']).agg('count')

SQ1.sort_index(ascending=False).plot(
    kind='barh',
    figsize=(10, 10),
    cmap='GnBu',
    edgecolor='black',
    title='SQ by Age', 
    fontsize=20,
    ).yaxis.label.set_visible(False)

'''

输出

在此处输入图像描述

将图表放入各个图表中以可视化分布

'''

def plot_counts_by_Age(groupby_count_obj, Age, ax=None):
    
    #Takes a count-aggregated groupby object, an age group, and an 
    (optional) AxesSubplot, and draws a barplot for that group
    
    sort_order = groupby_count_obj.loc[Age].sort_index().index

    sns.barplot(y = groupby_count_obj.loc[Age].index, 
                x = groupby_count_obj.loc[Age].values, 
                order = sort_order, 
                palette = 'GnBu', edgecolor = 'black', 
                ax = ax
                ).set_title("{}".format(Age))


# Setup for the 4x2 subplot grid
# Note we don't want to share the x axis since we have counts
fig, ax = plt.subplots(nrows=3, ncols=2, figsize=(10, 10), sharey=True)

# ax.flatten() avoids having to explicitly reference a subplot index in ax
# Use consideration_grouped.index[:-1] because we're not plotting the under-30s
for subplot, Age in zip(ax.flatten(), list(SQ1.index)):
    plot_counts_by_Age(SQ1, Age, ax=subplot)
    
plt.tight_layout()

'''

输出

在此处输入图像描述

如果可能的话,我需要在每个栏中包含百分比细分

谢谢!

Is it possible to include percentage breakdown in a bar chart in Python?

This is my code, I want to filter frequency of words by level of income

'''

Q1 = data.iloc[:, 5:14]

SQ1 =Q1.groupby(data['Income']).agg('count')

SQ1.sort_index(ascending=False).plot(
    kind='barh',
    figsize=(10, 10),
    cmap='GnBu',
    edgecolor='black',
    title='SQ by Age', 
    fontsize=20,
    ).yaxis.label.set_visible(False)

'''

Output

enter image description here

Graph put into individual graphs to visualise distribution

'''

def plot_counts_by_Age(groupby_count_obj, Age, ax=None):
    
    #Takes a count-aggregated groupby object, an age group, and an 
    (optional) AxesSubplot, and draws a barplot for that group
    
    sort_order = groupby_count_obj.loc[Age].sort_index().index

    sns.barplot(y = groupby_count_obj.loc[Age].index, 
                x = groupby_count_obj.loc[Age].values, 
                order = sort_order, 
                palette = 'GnBu', edgecolor = 'black', 
                ax = ax
                ).set_title("{}".format(Age))


# Setup for the 4x2 subplot grid
# Note we don't want to share the x axis since we have counts
fig, ax = plt.subplots(nrows=3, ncols=2, figsize=(10, 10), sharey=True)

# ax.flatten() avoids having to explicitly reference a subplot index in ax
# Use consideration_grouped.index[:-1] because we're not plotting the under-30s
for subplot, Age in zip(ax.flatten(), list(SQ1.index)):
    plot_counts_by_Age(SQ1, Age, ax=subplot)
    
plt.tight_layout()

'''

Output

enter image description here

I need to include percentage breakdown in each bar if possible

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文