matplotlib.pylab.hist 中的 histt​​ype='bar' / 'stepfilled' / 'barstacked' 有什么区别?

Cha*_*Loi 5 python matplotlib

习惯 plt.hist。但是,我认为 histt​​ype='bar' / 'stepfilled' / 'barstacked' 之间没有区别。这是我的试用代码

import numpy as np
import matplotlib.pylab as plt

x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)

fig ,ax=plt.subplots(3)
kwargs = dict(alpha=0.3, normed=True, bins=40)
ax[0].hist(x1, **kwargs)
ax[0].hist(x2, **kwargs)
ax[0].hist(x3, **kwargs)


kwargs1 = dict(histtype='stepfilled', alpha=0.3, normed=True, bins=40)
ax[1].hist(x1, **kwargs1)
ax[1].hist(x2, **kwargs1)
ax[1].hist(x3, **kwargs1)

kwargs2 = dict(histtype='barstacked', alpha=0.3, normed=True, bins=40)
ax[2].hist(x1, **kwargs2)
ax[2].hist(x2, **kwargs2)
ax[2].hist(x3, **kwargs2)

plt.show()
Run Code Online (Sandbox Code Playgroud)

这就是结果,根本没有区别在此输入图像描述

gmd*_*mds 6

原因是,histtype仅当您将多组数据传递给hist,但您已对hist每个数据集进行了 9 次单独的调用时才适用。

将您的结果与组合数据集时发生的结果进行比较:

import numpy as np
import matplotlib.pylab as plt

x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)

data = [x1, x2, x3]

fig, ax = plt.subplots(3)

ax[0].hist(data, alpha=0.3, normed=True, bins=40)
ax[1].hist(data, histtype='stepfilled', alpha=0.3, normed=True, bins=40)
ax[2].hist(data, histtype='barstacked', alpha=0.3, normed=True, bins=40)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述