我有兩個分組條形圖,顯示3組中兩個系統之間的數值變化,如下圖。在這里,我能夠使用下面的代碼將資料標簽添加到條形圖中(所產生的圖附后)
我想做的是在頂部(或底部的負值變化情況),添加一個額外的資料標簽,以捕捉價值變化的百分比,如第二個圖中的紅色33%(我用手編輯的)。我如何從這段代碼中實作這一點?先謝謝你。
importmatplotlib.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)
圖, (ax0, ax1) = plt. subplots(nrows=1, ncols=2, sharey=True, figsize=(18,10)
labels = ['Group 1', 'Group 2', 'Group 3']
x = np.arange(len(labsels))
ax0.set_ylabel('Change in Values', va='center', rotation='vertical',
fontsize=17, fontweight='bold')
width = 0.28 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(labsels,fontsize=15)
rects1 = ax0.bar(x-width/2, change_case0_to_case1_system1, width, label='case 0 to Case 1',
color='#292929', edgecolor='black', lineewidth=1)
rects2 = ax0.bar(x width/2, change_case1_to_case2_system1, width, label='case 1 to case 2',
color='#7f6d5f', edgecolor='black', lineewidth=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(lags,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()
uj5u.com熱心網友回復:
- 額外的情節格式化的代碼已經被遺漏了,因為它與答案不相關。它可以根據你的要求被添加回來。
- 每個
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/328938.html標籤:
