我试着把每个文本放在各自的位置,但文本出来的位置跳跃,从而举例说明,有15组酒吧在图表中,但只有8个有他们的命名在x轴上。
每组条形图都有自己的名称。
andar = ['0', '23º andar', '22º andar', '21º andar', '20º andar', '19º andar', '18º andar', '17º andar',
'16º andar', '15º andar', '14º andar', '13º andar', '12º andar', '11º andar', 'Térreo A', 'Térreo B']
ano_1=[285, 107, 38, 61, 62, 93, 45, 68, 88, 60, 77, 61, 60, 457, 34,]
ano_2=[336, 38, 24, 72, 64, 92, 52, 72, 94, 63, 80, 41, 65, 431, 45]
ano_3=[277, 12, 21, 19, 38, 86, 37, 45, 68, 55, 61, 15, 43, 431, 27]
ano_4=[261, 8, 2, 4, 46, 91, 33, 41, 61, 55, 55, 22, 41, 452, 21]
ano_5=[222, 2, 2, 6, 46, 80, 33, 41, 63, 61, 57, 26, 39, 457, 14]
barWidth = 0.14 # the width of the bars
fig, ax = plt.subplots(constrained_layout=True, figsize=(30, 12))
rects1 = np.arange(len(ano_1))
rects2 = [x + barWidth for x in rects1]
rects3 = [x + barWidth for x in rects2]
rects4 = [x + barWidth for x in rects3]
rects5 = [x + barWidth for x in rects4]
plt.bar(rects1, ano_1, width=barWidth, label = 2018)
plt.bar(rects2, ano_2, width=barWidth, label = 2019)
plt.bar(rects3, ano_3, width=barWidth, label = 2020)
plt.bar(rects4, ano_4, width=barWidth, label = 2021)
plt.bar(rects5, ano_5, width=barWidth, label = 2022)
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Consumo')
ax.set_title('Consumo de energia por andar')
ax.set_xticklabels(labels = andar, rotation = 45)
ax.legend()
fig.tight_layout()
1条答案
按热度按时间mtb9vblg1#
删除
andar
中的'0'
并添加ax.set_xticks(np.arange(len(andar)))
。代码: