在 pandas 条形图中添加带有占位符 xticks 的空白 space
Add blank space with placeholder xticks in pandas bar plot
我正在寻找一种方法来在我的这个条形图上添加一个 'place holder' 值。问题是该组没有 Quintile 1,但我仍想用“1”勾号显示它,留空,然后由剩余数据引导。这是它的样子:
这是我的代码:
df_temp = df1.loc[df1['Rep Segment_INSIDE'] == 1]
output2 = df_temp.groupby(['Quintile'])['Productivity Score', 'Funnel', 'Attainment',
'Activity', 'Forecast Accuracy', 'Training'].mean()
output2 = output2.sort_values('Productivity Score', ascending = False)
output2 = output2.drop(columns = ['Productivity Score'], axis = 1)
output2.plot.bar(stacked = True, grid=True, rot = 0)
plt.title('Segment Inside: Average KPI Metrics', fontsize= 25)
plt.xlabel('Quintiles', fontsize = 25)
plt.ylabel('Average KPI Scores, accumulated Productivity Score (%)', fontsize = 25)
plt.xticks(fontsize = 20)
plt.yticks(np.arange(0,1.1, 0.1), fontsize = 20)
plt.legend(loc=1, prop={'size': 30})
本质上,条形图应该向右移动,刻度为 1-5,1 应该是空的。感谢您提供的任何帮助。
附加支线任务:
如果有人知道如何为这些五分位数中的每一个添加成对的条形图,它将是堆叠条形图的高度,表示总分,我也一直在尝试解决这个问题。
要添加占位符刻度,reindex
针对整个五分位范围:
output2 = output2.reindex(range(1, 6))
# Funnel Attainment Activity Accuracy Training
# Quintiles
# 1 NaN NaN NaN NaN NaN
# 2 0.210 0.075 0.040 0.125 0.155
# 3 0.155 0.010 0.200 0.150 0.135
# 4 0.125 0.015 0.160 0.240 0.135
# 5 0.065 0.000 0.225 0.180 0.230
output2.plot.bar(stacked=True, grid=True, rot=0)
要标记堆栈总数, 百分比 f-string:
for x, y in enumerate(output2.sum(axis=1)):
ax.annotate(f'{y:.0%}', (x, y), ha='center')
我正在寻找一种方法来在我的这个条形图上添加一个 'place holder' 值。问题是该组没有 Quintile 1,但我仍想用“1”勾号显示它,留空,然后由剩余数据引导。这是它的样子:
这是我的代码:
df_temp = df1.loc[df1['Rep Segment_INSIDE'] == 1]
output2 = df_temp.groupby(['Quintile'])['Productivity Score', 'Funnel', 'Attainment',
'Activity', 'Forecast Accuracy', 'Training'].mean()
output2 = output2.sort_values('Productivity Score', ascending = False)
output2 = output2.drop(columns = ['Productivity Score'], axis = 1)
output2.plot.bar(stacked = True, grid=True, rot = 0)
plt.title('Segment Inside: Average KPI Metrics', fontsize= 25)
plt.xlabel('Quintiles', fontsize = 25)
plt.ylabel('Average KPI Scores, accumulated Productivity Score (%)', fontsize = 25)
plt.xticks(fontsize = 20)
plt.yticks(np.arange(0,1.1, 0.1), fontsize = 20)
plt.legend(loc=1, prop={'size': 30})
本质上,条形图应该向右移动,刻度为 1-5,1 应该是空的。感谢您提供的任何帮助。
附加支线任务: 如果有人知道如何为这些五分位数中的每一个添加成对的条形图,它将是堆叠条形图的高度,表示总分,我也一直在尝试解决这个问题。
要添加占位符刻度,reindex
针对整个五分位范围:
output2 = output2.reindex(range(1, 6))
# Funnel Attainment Activity Accuracy Training
# Quintiles
# 1 NaN NaN NaN NaN NaN
# 2 0.210 0.075 0.040 0.125 0.155
# 3 0.155 0.010 0.200 0.150 0.135
# 4 0.125 0.015 0.160 0.240 0.135
# 5 0.065 0.000 0.225 0.180 0.230
output2.plot.bar(stacked=True, grid=True, rot=0)
要标记堆栈总数,
for x, y in enumerate(output2.sum(axis=1)):
ax.annotate(f'{y:.0%}', (x, y), ha='center')