如何在matplotlib中的条形图中添加多个数据标签

2024-09-28 21:36:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个分组条形图,显示了三组中两个系统案例之间的价值变化,如下所示。在这里,我可以使用下面的代码(附图)将数据标签添加到条中Figure produced from code

我想做的是在顶部(或者在值的负变化情况下在底部),添加一个额外的数据标签,该标签捕获值变化的百分比,如第二个图所示,其中33%为红色(我手工编辑)。如何从这段代码中实现这一点?提前谢谢你。Figure I want to achieve

import matplotlib.pyplot as plt
import numpy as np

value_case0_system1 = np.array([30, 20, 40])
value_case1_system1 = np.array([20, 25, 50])
value_case2_system1 = np.array([10, 35, 45])

value_case1_system2 = np.array([60, 50, 40])
value_case2_system2 = np.array([50, 40, 55])

change_case0_to_case1_system1 = np.subtract(value_case1_system1,value_case0_system1)
change_case1_to_case2_system1 = np.subtract(value_case2_system1,value_case1_system1)
change_case1_to_case2_system2 = np.subtract(value_case2_system2,value_case1_system2)

fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, sharey=True, figsize=(18,10))
labels = ['Group 1', 'Group 2', 'Group 3']
x = np.arange(len(labels))
ax0.set_ylabel('Change in Values', va='center', rotation='vertical',
         fontsize=17, fontweight='bold')
width = 0.28
ax0.set_title('System 1', fontsize=17, fontweight='bold')
ax0.axhline(y=0, color='black', ls=':', lw=2)
ax0.set_xticks(x)
ax0.set_xticklabels(labels,fontsize=15)
rects1 = ax0.bar(x-width/2, change_case0_to_case1_system1, width, label='Case 0 to Case 1',
        color='#292929', edgecolor='black', linewidth=1)
rects2 = ax0.bar(x+width/2, change_case1_to_case2_system1, width, label='Case 1 to Case 2',
        color='#7f6d5f', edgecolor='black', linewidth=1)
ax0.bar_label(rects1, padding=3, fontsize=11)
ax0.bar_label(rects2, padding=3, fontsize=11)
leg = ax0.legend(loc="upper left", bbox_to_anchor=[0, 1],
                 ncol=1, fancybox=True)
ax0.legend(fontsize=15)

ax1.set_title('System 2', fontsize=17, fontweight='bold')
ax1.axhline(y=0, color='black', ls=':', lw=2)
ax1.set_xticks(x)
ax1.set_xticklabels(labels,fontsize=15)

rects3 = ax1.bar(x, change_case1_to_case2_system2, width, label='Case 1 to Case 2',
        color='#7f6d5f', edgecolor='black', linewidth=1)
ax1.legend(shadow=True, fancybox=True)
ax1.bar_label(rects3, padding=3, fontsize=11)
leg = ax1.legend(loc="upper left", bbox_to_anchor=[0, 1],
                 ncol=1, fancybox=True)
ax1.legend(fontsize=15)
plt.tight_layout()
plt.show()

Tags: tovaluenpbarchangewidthlabelset
1条回答
网友
1楼 · 发布于 2024-09-28 21:36:33
  • 额外打印格式的代码已被省略,因为它与答案无关。可以根据您的要求重新添加
  • 每个^{}对标签进行全局着色,因此与此answer不同,需要为百分比变化添加第二个.bar_label,使用不同的colorpadding
  • 对于每个案例,计算百分比变化,并在列表中设置字符串格式。
    • 将字符串格式的计算列表设置为.bar_label中的labels参数
  • 给定OP中的代码,需要添加6行代码,3行用于创建标签列表,3行用于将标签添加到绘图
  • 额外资源:
  • python 3.8.11matplotlib 3.4.3
change_case0_to_case1_system1 = np.subtract(value_case1_system1, value_case0_system1)
# add list of string formatted percent change calculation
per_change_case0_to_case1_system1 = [f'({v}%)' for v in (change_case0_to_case1_system1 / value_case0_system1).round(2)*100]

change_case1_to_case2_system1 = np.subtract(value_case2_system1, value_case1_system1)
# add list of string formatted percent change calculation
per_change_case1_to_case2_system1 = [f'({v}%)' for v in (change_case1_to_case2_system1 / value_case1_system1).round(2)*100]

change_case1_to_case2_system2 = np.subtract(value_case2_system2, value_case1_system2)
# add list of string formatted percent change calculation
per_case1_to_case2_system2 = [f'({v}%)' for v in (change_case1_to_case2_system2 / value_case1_system2).round(2)*100]

fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, sharey=True, figsize=(18,10))
labels = ['Group 1', 'Group 2', 'Group 3']
x = np.arange(len(labels))

width = 0.28

ax0.set_xticks(x)
ax0.set_xticklabels(labels, fontsize=15)

rects1 = ax0.bar(x-width/2, change_case0_to_case1_system1, width, label='Case 0 to Case 1', color='#292929', edgecolor='black', linewidth=1)
rects2 = ax0.bar(x+width/2, change_case1_to_case2_system1, width, label='Case 1 to Case 2', color='#7f6d5f', edgecolor='black', linewidth=1)

ax0.bar_label(rects1, padding=3, fontsize=11)
# add a second annotation with the string formatted labels
ax0.bar_label(rects1, labels=per_change_case0_to_case1_system1, padding=15, fontsize=11, color='red')

ax0.bar_label(rects2, padding=3, fontsize=11)
# add a second annotation with the string formatted labels
ax0.bar_label(rects2, labels=per_change_case1_to_case2_system1, padding=15, fontsize=11, color='red')

rects3 = ax1.bar(x, change_case1_to_case2_system2, width, label='Case 1 to Case 2', color='#7f6d5f', edgecolor='black', linewidth=1)

ax1.set_xticks(x)
ax1.set_xticklabels(labels,fontsize=15)

ax1.bar_label(rects3, padding=3, fontsize=11)
# add a second annotation with the string formatted labels
ax1.bar_label(rects3, labels=per_case1_to_case2_system2, padding=15, fontsize=11, color='red')

plt.tight_layout()
plt.show()

enter image description here

相关问题 更多 >