matplotlib xtick标签未对齐

时间:2014-06-04 15:17:58

标签: python matplotlib bar-chart labels

我创建了一个非常简单的堆积条形图,但由于某种原因,我的x-tick标签偏离中心。我尝试使用一些tick.label.set_属性来排列它们无济于事。有什么建议?谢谢!

代码:

bottom = np.vstack((np.zeros((data.shape[1],), dtype=data.dtype), np.cumsum(data, axis=0) [:-1]))
for dat, col, bot, lab in zip(data, colors, bottom, labels):
    ax1.bar(ind, dat, color=col, bottom = bot, alpha = .7, label = lab)

xticks([z for z in range(0,len(income_brackets))],[(str("{0:.0f}".format(k/1000)) + '-' + str("{0:.0f}".format(l/1000))) for k,l in income_brackets])
for tick in ax1.xaxis.get_major_ticks():
    tick.label.set_fontsize(12)
    tick.label.set_horizontalalignment('left')

图像:

enter image description here

1 个答案:

答案 0 :(得分:6)

在条形图中使用align="center",默认情况下它们在边缘排列。这是一个最小的工作示例:

import pylab as plt

X = [1,2,3,4]
Y = [1,4,3,2]

fig, axes = plt.subplots(2, 1)
ax1, ax2 = axes

ax1.bar(X,Y,alpha=.5)
ax1.set_xticks(X)

ax2.bar(X,Y,align="center",alpha=.5)
ax2.set_xticks(X)           

plt.show()

enter image description here