簡體   English   中英

更改Python直方圖bin中的計數

[英]change the count in a Python histogram bin

我有一個Python直方圖。

我想將直方圖的峰值標准化為1,以便只有條形的相對高度很重要。

我看到一些這樣做的方法涉及更改bin寬度,但我不想這樣做。

我也意識到我可以只更改y軸的標簽,但我還有另一個圖重疊,因此yticks必須是實際值。

有沒有辦法訪問和更改每個bin中的直方圖“count”?

謝謝。

我認為你所追求的是直方圖的標准化形式,其中y軸是密度而不是計數。 如果你正在使用Numpy,只需在直方圖功能中使用normed標志。

如果您希望直方圖的峰值為1,那么您可以將每個bin中的計數除以最大bin值,即( 在此處構建SO MatPlotLib示例):

#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np

# Generate random data
mu, sigma = 200, 25
x = mu + sigma*np.random.randn(10000)

# Create the histogram and normalize the counts to 1
hist, bins = np.histogram(x, bins = 50)
max_val = max(hist)
hist = [ float(n)/max_val for n in hist]

# Plot the resulting histogram
center = (bins[:-1]+bins[1:])/2
width = 0.7*(bins[1]-bins[0])
plt.bar(center, hist, align = 'center', width = width)
plt.show()

在此輸入圖像描述

暫無
暫無

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

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