【发布时间】:2022-01-11 19:36:24
【问题描述】:
我有以下代码
import numpy as np
import matplotlib.pyplot as plt
oct_data = [10, 24, 25, 30]
nov_data = [12, 42, 21, 78]
labels = ['Account_1', 'Account_2', 'Account_3', 'Account_4']
bar_width = 0.4
rect_1 = np.arange(0, len(oct_data)*2 ,2)
rect_2 = [x + bar_width for x in rect_1]
plt.bar(rect_1, oct_data, color='#7f6d5f', width=bar_width, edgecolor='white', label='Month_1')
plt.bar(rect_2, nov_data, color='#557f2d', width=bar_width, edgecolor='white', label='Month_2')
plt.ylabel('Cost ($)', fontsize=10)
plt.legend()
plt.show()
如您所见,我的xticks (Account_1, Account_2, ...) 没有居中。
据我了解,这个命令应该可以完成这项工作,但它没有。
plt.xticks([r + bar_width for r in range(0, len(oct_data)*2, 2)], labels)
我还想在条形图内添加高度值。通常,这就是我使用“单条”图的方式:
for i in range(len(labels)):
plt.text(i, oct_data[i]//2, oct_data[i], ha = 'center', color = 'black')
但这在这里不起作用。
任何帮助将不胜感激。我是 Matplotlib 的初学者。
【问题讨论】:
标签: python matplotlib annotations bar-chart