簡體   English   中英

在散點圖上放置直方圖

[英]Placing a histogram over scatter plot

我想知道在matplotlib中是否有一種方法可以放置直方圖以覆蓋散點圖在波紋管中的點的高度? 我只是在尋找一種制作直方圖以覆蓋軸上10個bin的方法。 這是我到目前為止的代碼:

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()

在此處輸入圖片說明

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()

得到:

在此處輸入圖片說明

因此,顯然,答案是“是”。 現在,直方圖特征需要根據您的需要進行調整。 在上面的示例中,它使用與散布數據相同的X和Y比例尺,但是不必如此。


還是您正在尋找一種使用直方圖數據繪制條形圖的方法? 然后:

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.bar(candid-.8/2, candyn, width=.8)
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()

在此處輸入圖片說明

順便說一下, 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)以獲得對縮放的控制。 :如果您手動設置刻度,則也應手動設置范圍。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM