【发布时间】:2016-12-30 09:14:50
【问题描述】:
在 Matplotlib 中创建堆叠直方图时,我注意到 bin 宽度缩小了。在这个简单的例子中:
import numpy as np
import matplotlib
import matplotlib.pylab as plt
#Create histograms and plots
fig = plt.figure()
gs = matplotlib.gridspec.GridSpec(1, 2)
h1 = fig.add_subplot(gs[0])
h2 = fig.add_subplot(gs[1])
x = np.random.normal(0, 5, 500)
y = np.random.normal(0, 20, 500)
bins = np.arange(-60,60, 5)
h1.hist([x, y], bins=bins, stacked=True)
h2.hist(x, bins=bins, alpha=1)
h2.hist(y, bins=bins, alpha=0.5)
plt.tight_layout()
plt.show()
filename = 'sample.pdf'
plt.savefig(filename)
我得到以下输出:
请注意,左侧的直方图在每个 bin 之间都有间距,即使左右直方图都使用相同的 bin。
有没有办法纠正这种行为?我希望左侧的直方图使用完整的 bin 宽度,以便相邻的 bin 共享一条边。
【问题讨论】:
标签: python matplotlib