python 画 直方图

# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

from scipy.stats import norm

mu = 100  # 正态分布的均值
sigma = 15  # 标准差
x = mu + sigma * np.random.randn (10000)  # 在均值周围产生符合正态分布的x值

num_bins = 50
n, bins, patches = plt.hist (x, num_bins, density=True, facecolor='green', alpha=0.5)
# 直方图函数,x为x轴的值,normed=1表示为概率密度,即和为一,绿色方块,色深参数0.5.返回n个概率,直方块左边线的x值,及各个方块对象
y = norm.pdf (bins, mu, sigma)  # 画一条逼近的曲线
plt.plot (bins, y, 'r--')
plt.xlabel ('Smarts')
plt.ylabel ('Probability')
plt.title (r'Histogram of IQ: $\mu=100$, $\sigma=15$')  # 中文标题 u'xxx'

plt.subplots_adjust (left=0.15)  # 左边距
plt.show ()


# import numpy as np
# import matplotlib.pyplot as plt
#
# from scipy.stats import norm
# mu = 100    # 均值
# sigma = 15  # 标准

你可能感兴趣的:(python,python,matplotlib,开发语言,3d)