[英]No outlines on bins of Matplotlib histograms or Seaborn distplots
[英]Call upon individual bins in histograms in Matplotlib?
我需要使用三個直方圖的每個單獨的 bin 來在等式中使用(即來自 histogram1、histogram2 和 histogram3 的 bin1,然后來自 h1、h2、h3 等的 bin2)。
有什么方法可以從直方圖中調用各個 bin 來使用嗎?
提前致謝
是的,當您調用plt.hist
時,會返回 bin 的位置以及每個 bin 中的條目數。 假設您生成了三個直方圖(我將使用直方圖 0、1 和 2,因為 Python):
import matplotlib.pyplot as plt
import numpy as np
x0 = np.random.rand(25)
x1 = np.random.rand(25)
x2 = np.random.rand(25)
counts0, bins0, patches0 = plt.hist(x0)
counts1, bins1, patches1 = plt.hist(x1)
counts2, bins2, patches2 = plt.hist(x2)
然后將直方圖 0 的 bin 位置存儲在bins0
。
然后將直方圖 0 的 bin 中的條目數存儲在counts0
。
然后我很想將這些收集到二維數組中:
counts = np.vstack([counts0, counts1, counts2]).T
bins = np.vstack([bins0, bins1, bins2]).T
現在, bins[i, j]
詳細說明了直方圖j
中 bin i
的位置。 同樣counts[i, j]
包含直方圖j
bin i
中的條目數。
通過此設置,您可以獲得 bin i
中直方圖 0、1 和 2 的counts[i]
作為counts[i]
。
此外,如果您實際上並不需要這些圖,並且只是調用plt.hist
來處理counts
和bins
那么您可以改用np.histogram
。 語法類似: counts, bins = np.histogram(x)
( np.histogram
不返回補丁)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.