[英]Arrange the chart's x-axis
我尝试将每个文本放在各自的位置,但文本在 position 中跳转,从而举例说明图表中有 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()
删除andar
中'0'
并添加ax.set_xticks(np.arange(len(andar)))
。
import numpy as np
import matplotlib.pyplot as plt
andar = ['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_xticks(np.arange(len(andar)))
ax.set_xticklabels(labels=andar, rotation=45)
ax.legend()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.