#!/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()
我想你想要的是一个标准化的直方图,其中y轴是密度而不是计数。如果使用的是Numpy,只需使用histogram function中的
normed
标志。在如果希望直方图的峰值为1,则可以将每个bin中的计数除以最大bin值,即(根据SO MatPlotLib示例here)除以:
相关问题 更多 >
编程相关推荐