matplotlib轴标签未与数据箱对齐

时间:2015-05-08 17:58:38

标签: python matplotlib label axes

我试图绘制一个相关矩阵而不是bin号作为标签(参见y轴标签)我希望有物理的,实际的单位(x轴标签)。不幸的是,我无法将xticks与垃圾箱的中心对齐,我不明白为什么我在矩阵的每一列都没有标签。

"5.01" is the last bin label, not the second to last

下面的代码生成图:

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
labels =['', '0.13','0.20','0.32','0.50','0.79','1.26','2.00','3.16','5.01']
plt.xticks(range(len(labels)+1), labels, size='small')
#The '+1 in range seems needed as if I don't include it the first label is not displayed at all
plt.imshow(np.corrcoef(bootstrap_samples_matrix.transpose()), interpolation='nearest', cmap=plt.cm.Reds, extent=(0.5,10.5,0.5,10.5), align="center")
plt.colorbar()

2 个答案:

答案 0 :(得分:0)

您将xticks设置为标签定义的位置,但实际上它们并不相同。我想你想把标签设置到实际位置,就像这样;

替换:plt.xticks(范围(len(标签)+1),标签,尺寸='小')

使用:ax.set_xticklabels(标签,次要=假)

答案 1 :(得分:0)

要将xticks与区域的中心对齐,并且为了在矩阵的每一列上都有标签,可以通过手动设置标签来完成。

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

manual_labels =['0.20','0.32','0.50','0.79','1.26','2.00','3.16']
ax.set_xticks([0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5])
ax.set_xticklabels(manual_labels, minor=False)

ax.set_yticks([0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5])
ax.set_yticklabels(manual_labels, minor=False)