Python:绘制正态分布时,钟形曲线和直方图看起来很奇怪

时间:2019-11-13 19:07:23

标签: python matplotlib

我有一个包含一些数据的数组,我想用钟形曲线(正态分布)绘制直方图。但是,正态分布曲线远低于直方图。我的代码有什么问题吗?

下面是源代码:

import matplotlib.pyplot as plt
from scipy.stats import norm
import numpy as np

data = [14, 58, 43, 81, 30, 418, 83, 14, 58, 14, 35, 59, 213, 37, 88, 14, 59, 15, 94, 16, 54, 14, 214, 15, 43, 99, 14, 58, 14, 67, 11, 179,
16, 98, 15, 772, 57, 15, 65, 13, 344, 17, 43, 215, 15, 102, 14, 64, 17, 55, 12, 131, 20, 101, 14, 193, 16, 49, 88, 14, 90, 16, 92, 17, 39, 56, 15, 48, 176,
14, 63, 13, 86, 14, 54, 14, 29, 93, 14, 44, 147, 11, 79, 51, 35, 296, 162, 16, 126, 15, 45, 88, 15, 99, 16, 42, 98, 14, 54, 13, 34, 135, 11, 35, 58, 14, 56,
17, 51, 130, 14, 254, 14, 131, 15, 91, 15, 58, 15, 92, 14, 137, 17, 372, 53, 44, 130, 13, 92, 12, 33, 44, 46, 55, 20, 89, 14, 58, 14, 40, 55, 14, 124, 14, 
42, 43, 157, 15, 42, 176, 16, 128, 16, 84, 14, 47, 184, 13, 49, 87, 15, 59, 15, 62, 15, 95, 16, 58, 14, 62, 13, 296, 296, 510, 172, 11, 33, 256, 15, 131, 18,
391, 15, 501, 91, 11, 49, 31, 44, 87, 14, 45, 56, 14, 90, 16, 43, 57, 19, 49, 57, 13, 43, 56, 15, 54, 14, 63, 5, 32, 36, 46, 95, 14, 65, 13, 80, 59, 14, 67, 
14, 51, 128, 15, 43, 46, 42, 88, 14, 306, 91, 14, 475, 57, 14, 84, 13, 43, 57, 14, 41, 42, 130, 14, 91, 16, 45, 87, 16, 43, 126, 15, 68, 90, 14, 92, 62, 84, 
15, 45, 43, 140, 14, 38, 57, 16, 46, 43, 173, 13, 344, 41, 56, 14, 53, 12, 39, 42, 87, 14, 42, 87, 14, 46, 169, 16, 42, 45, 41, 771, 333, 58, 15, 563, 59, 13,
37, 42, 56, 15, 37, 155, 15, 42, 60, 14, 65, 12, 500, 151, 15, 48, 88, 14, 43, 88, 14, 56, 18, 60, 13, 97, 14, 42, 174, 14, 43, 43, 131, 14, 43, 89, 15, 58, 
14, 50, 89, 14, 39, 43, 87, 16, 125, 14, 41, 44, 41, 172, 16, 45, 90, 13, 43, 43, 57, 15, 98, 17, 38, 45, 238, 14, 37, 58, 15, 126, 15, 740, 43, 141, 58, 14]

# fit a normal distribution to the data
mean, std = norm.fit(all_intervals_list)
print("mean=%.2f, std=%.2f" % (mean, std))

# Turn on the grid
ax = plt.gca()
ax.grid() 
# Turn on the minor TICKS, which are required for the minor GRID
ax.minorticks_on()
# Customize the major grid
ax.grid(which='major', linestyle='-', linewidth='0.5', color='#0b559f')
# Customize the minor grid
ax.grid(which='minor', linestyle=':', linewidth='0.5', color='black')

# Plot the histogram. '''
plt.hist(data, bins=50, density=True, alpha=0.6, color='#0b559f', edgecolor='black')

#  Plot the pdf
x_min = mean - 600
x_max = mean + 600
x = np.linspace(x_min, x_max, 50, dtype=int)
y = norm.pdf(x, loc=mean, scale=std)
plt.xlim(x_min,x_max)
plt.plot(x, y, linewidth=2, color='coral')

plt.title('Data normal distribution',fontsize=10)
plt.xlabel('Interval (in seconds)')
plt.ylabel('Normal Distribution')

# display and save the png
plt.title('Data distribution',fontsize=10)
plt.xlabel('Intervals (in seconds)')
plt.ylabel('Normal Distribution')
plt.show(block=False)
plt.pause(10)
plt.savefig("normal_distribution.png")
plt.close()

enter image description here

0 个答案:

没有答案
相关问题