numpy 排列图表的X轴

mwg9r5ms  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(123)

我试着把每个文本放在各自的位置,但文本出来的位置跳跃,从而举例说明,有15组酒吧在图表中,但只有8个有他们的命名在x轴上。
每组条形图都有自己的名称。

  1. andar = ['0', '23º andar', '22º andar', '21º andar', '20º andar', '19º andar', '18º andar', '17º andar',
  2. '16º andar', '15º andar', '14º andar', '13º andar', '12º andar', '11º andar', 'Térreo A', 'Térreo B']
  3. ano_1=[285, 107, 38, 61, 62, 93, 45, 68, 88, 60, 77, 61, 60, 457, 34,]
  4. ano_2=[336, 38, 24, 72, 64, 92, 52, 72, 94, 63, 80, 41, 65, 431, 45]
  5. ano_3=[277, 12, 21, 19, 38, 86, 37, 45, 68, 55, 61, 15, 43, 431, 27]
  6. ano_4=[261, 8, 2, 4, 46, 91, 33, 41, 61, 55, 55, 22, 41, 452, 21]
  7. ano_5=[222, 2, 2, 6, 46, 80, 33, 41, 63, 61, 57, 26, 39, 457, 14]
  8. barWidth = 0.14 # the width of the bars
  9. fig, ax = plt.subplots(constrained_layout=True, figsize=(30, 12))
  10. rects1 = np.arange(len(ano_1))
  11. rects2 = [x + barWidth for x in rects1]
  12. rects3 = [x + barWidth for x in rects2]
  13. rects4 = [x + barWidth for x in rects3]
  14. rects5 = [x + barWidth for x in rects4]
  15. plt.bar(rects1, ano_1, width=barWidth, label = 2018)
  16. plt.bar(rects2, ano_2, width=barWidth, label = 2019)
  17. plt.bar(rects3, ano_3, width=barWidth, label = 2020)
  18. plt.bar(rects4, ano_4, width=barWidth, label = 2021)
  19. plt.bar(rects5, ano_5, width=barWidth, label = 2022)
  20. # Add some text for labels, title and custom x-axis tick labels, etc.
  21. ax.set_ylabel('Consumo')
  22. ax.set_title('Consumo de energia por andar')
  23. ax.set_xticklabels(labels = andar, rotation = 45)
  24. ax.legend()
  25. fig.tight_layout()

mtb9vblg

mtb9vblg1#

删除andar中的'0'并添加ax.set_xticks(np.arange(len(andar)))

代码:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. andar = ['23º andar', '22º andar', '21º andar', '20º andar', '19º andar', '18º andar', '17º andar',
  4. '16º andar', '15º andar', '14º andar', '13º andar', '12º andar', '11º andar', 'Térreo A', 'Térreo B']
  5. ano_1 = [285, 107, 38, 61, 62, 93, 45, 68, 88, 60, 77, 61, 60, 457, 34, ]
  6. ano_2 = [336, 38, 24, 72, 64, 92, 52, 72, 94, 63, 80, 41, 65, 431, 45]
  7. ano_3 = [277, 12, 21, 19, 38, 86, 37, 45, 68, 55, 61, 15, 43, 431, 27]
  8. ano_4 = [261, 8, 2, 4, 46, 91, 33, 41, 61, 55, 55, 22, 41, 452, 21]
  9. ano_5 = [222, 2, 2, 6, 46, 80, 33, 41, 63, 61, 57, 26, 39, 457, 14]
  10. barWidth = 0.14 # the width of the bars
  11. fig, ax = plt.subplots(constrained_layout=True, figsize=(30, 12))
  12. rects1 = np.arange(len(ano_1))
  13. rects2 = [x + barWidth for x in rects1]
  14. rects3 = [x + barWidth for x in rects2]
  15. rects4 = [x + barWidth for x in rects3]
  16. rects5 = [x + barWidth for x in rects4]
  17. plt.bar(rects1, ano_1, width=barWidth, label=2018)
  18. plt.bar(rects2, ano_2, width=barWidth, label=2019)
  19. plt.bar(rects3, ano_3, width=barWidth, label=2020)
  20. plt.bar(rects4, ano_4, width=barWidth, label=2021)
  21. plt.bar(rects5, ano_5, width=barWidth, label=2022)
  22. # Add some text for labels, title and custom x-axis tick labels, etc.
  23. ax.set_ylabel('Consumo')
  24. ax.set_title('Consumo de energia por andar')
  25. ax.set_xticks(np.arange(len(andar)))
  26. ax.set_xticklabels(labels=andar, rotation=45)
  27. ax.legend()
  28. plt.show()
展开查看全部

相关问题