我想知道在matplotlib中是否有一种方法来放置一个柱状图,以覆盖下面图中散点图的高度?我只是想办法做一个柱状图来覆盖轴上的10个箱子。以下是我目前掌握的代码:
bglstat = np.array([9.0, 10.0, 2.0, 7.0, 7.0, 4.0])
candyn = np.array([2.0, 2.0, 1.0, 1.0, 1.0, 3.0, 1.0, 2.0, 1.0, 1.0])
candid = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
fig = plt.figure()
a2 = fig.add_subplot(111)
a2.scatter(candid, candyn, color='red')
a2.set_xlabel("Candidate Bulgeless Galaxy ID #")
a2.set_ylabel("Classified as Bulgeless")
a2.set_xticks([1,2,3,4,5,6,7,8,9,10])
plt.show()
给出:
所以,很明显,答案是“是的”。现在需要根据需要调整直方图特征。在上面的例子中,它使用了与散点数据相同的X和Y比例,但不一定是这样。在
或者你在寻找一种用直方图数据绘制条形图的方法?然后:
^{pr2}$顺便说一下,
set_xticks
看起来有点奇怪。您可以考虑使用set_xticks(candid)
来显示您的类别,或者使用set_xticks(np.arange(1,11))
来显式地设置刻度1..10。另外,我建议您添加一些代码来设置范围(例如a2.set_xlim(-1,11)
,a2.set_ylim(0, np.max(candyn) + 1)
)来控制缩放。(经验法则:如果手动设置记号,也应手动设置范围。)相关问题 更多 >
编程相关推荐