Matplotlib 直方图中的圆形条形
我在Python中使用 matplotlib.pyplot.hist()
来绘制直方图。如果我设置了明显的边框宽度,比如使用 histtype='step'
,那么每个柱子的上角会有点圆。我希望它们能是尖尖的矩形。我已经尝试过使用 solid_capstyle
这个参数,它可以影响线条图的形状,但在 hist()
中不起作用。有没有什么办法可以做到这一点?谢谢!
这是我最简单的自包含示例
import numpy as np
import matplotlib.pyplot as plt
mu = 200
sigma = 25
x = mu + sigma*np.random.randn(10000)
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(8, 4))
ax0.hist(x, 20, normed=1, histtype='step', facecolor='g', alpha=0.75, linewidth=4.)
ax0.set_title('step')
# Create a histogram by providing the bin edges (unequally spaced).
bins = [100, 150, 180, 195, 205, 220, 250, 300]
ax1.hist(x, bins, normed=1, histtype='step', rwidth=0.8, linewidth=4.)
ax1.set_title('unequal bins')
plt.tight_layout()
plt.savefig('test.png', dpi=200)
1 个回答
6
在MatPlotLib 1.4版本中,你可以控制这个属性。所以,我建议你升级一下,比如如果你用pip的话:
pip install --upgrade matplotlib
然后在调用hist
的时候,使用joinstyle
这个参数(['miter' | 'round' | 'bevel']
);比如:
ax0.hist(x, 20, normed=1, histtype='step',
facecolor='g', alpha=0.75, linewidth=4.,
joinstyle='miter')
需要注意的是,在MPL 1.4中,'miter'
(方角)似乎是默认选项,所以在你的情况下其实不需要特别指定这个参数。我在这里提到它是为了让你更清楚。