在 Python 的条形图中包含百分比细分
是否可以在 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
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
I need to include percentage breakdown in each bar if possible
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论