在散点p上放置直方图

2024-09-30 14:38:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道在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()

enter image description here


Tags: 方法a2高度matplotlibnpfigpltarray
1条回答
网友
1楼 · 发布于 2024-09-30 14:38:56
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])
a2.hist(candyn, bins = arange(-.5,10.5,1))
plt.show()

给出:

enter image description here

所以,很明显,答案是“是的”。现在需要根据需要调整直方图特征。在上面的例子中,它使用了与散点数据相同的X和Y比例,但不一定是这样。在


或者你在寻找一种用直方图数据绘制条形图的方法?然后:

^{pr2}$

enter image description here

顺便说一下,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))来控制缩放。(经验法则:如果手动设置记号,也应手动设置范围。)

相关问题 更多 >