在Matplotlib轴中更改X轴刻度标签

时间:2019-12-04 23:54:25

标签: python matplotlib

我正在使用轴在同一图中绘制两个归一化的直方图,并且希望有一个bin可以捕获任何高于6000的值。这是我到目前为止所做的:-

fig,ax=plt.subplots(1,2)
x1 = (60, 80, 1000, 7000, 8000)
x2 = (9000, 10000, 11000, 12000, 13000)


weights1= np.ones_like(x1)/float(len(x1))
weights2= np.ones_like(x2)/float(len(x2))

bins= np.arange(0, 6000, 100)
ax[0].hist(np.clip(x1, bins[0], bins[-1]),  bins=bins,  weights=weights1, 
           alpha =.5, label = "A", color = "blue")
ax[1].hist(np.clip(x2, bins[0], bins[-1]),  bins=bins,  weights=weights2, 
           alpha =.5, label = "B", red = "red")
plt.show()

这是我得到的: enter image description here

如何将x刻度标签从6000更改为6000+,以显示它捕获了所有高于此的观测值?

1 个答案:

答案 0 :(得分:1)

类似的事情应该起作用:

ticks, labels = plt.xticks()
labels[-1].set_text('6000+')
plt.xticks(ticks, labels)

更新: 您可以手动完成所有操作:

bins= np.arange(0, 6000, 100)
plt.sca(ax[0])
plt.hist(np.clip(x1, bins[0], bins[-1]),  bins=bins,  weights=weights1, 
           alpha =.5, label = "A", color = "blue")
plt.xticks([0,2000,4000,6000], ['0','2000','4000','6000+'])

plt.sca(ax[1])
plt.hist(np.clip(x2, bins[0], bins[-1]),  bins=bins,  weights=weights2, 
           alpha =.5, label = "B", color = "red")
plt.xticks([0,2000,4000,6000], ['0','2000','4000','6000+'])

从理论上讲,这也应该起作用,但是由于某种原因,它不能...如果有人可以阐明这个问题...?

plt.sca(ax[1])
plt.hist(np.clip(x2, bins[0], bins[-1]),  bins=bins,  weights=weights2, 
           alpha =.5, label = "B", color = "red")
ticks, labels = plt.xticks()
labels[-1].set_text('6000+')
plt.xticks(ticks, labels)