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

时间:2019-05-24 03:02:38

标签: 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()

这就是没有任何区别的结果enter image description here

1 个答案:

答案 0 :(得分:1)

原因是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()

enter image description here