簡體   English   中英

在matplotlib中,使用plot和hist的返回值繪制直方圖

[英]In matplotlib, plotting the histogram using plot and the returns of hist

為了測試hist的返回值,我想通過matplotlib通過plot使用它們。 hist提供以下返回:

import matplotlib.pyplot as plt
counts, bins, bars = plt.hist(x)

其中x是要繪制直方圖的數據向量。

我嘗試了以下語法

plt.plot(bins,counts)

我收到以下錯誤

Error: x and y must have the same first dimension, but have shapes (501,) and (500,)

謝謝您的回答。

plt.hist()的matplotlib文檔中:

垃圾箱:數組
垃圾箱的邊緣。 長度nbins + 1(nbins最后一個bin的左邊緣和右邊緣)。 即使傳入多個數據集也總是一個數組。

因此,返回的值binsbins數+ 1,因為它包括最后一個箱的左箱邊緣和右邊緣。

您可能不希望包括最后一個bin的右邊緣,因此可以對數組進行切片:

plt.plot(bins[:-1], counts)

嘗試這個:

import matplotlib.pyplot as plt
plt.hist(x)
plt.show()

我猜這是最簡單的一個。

暫無
暫無

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

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