matplotlib 直方图中的圆条

fri*_*hax 4 python matplotlib histogram

matplotlib.pyplot.hist()在 python 中用于直方图。如果我使用可见的边宽,例如在使用 时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)
Run Code Online (Sandbox Code Playgroud)

far*_*rth 6

MatPlotLib 1.4 中提供了控制此属性的功能。所以,我建议升级;例如,如果您使用 pip:

pip install --upgrade matplotlib
Run Code Online (Sandbox Code Playgroud)

然后在调用中使用joinstyle关键字参数 ( ['miter' | 'round' | 'bevel']) hist; 例如:

ax0.hist(x, 20, normed=1, histtype='step',
         facecolor='g', alpha=0.75, linewidth=4.,
         joinstyle='miter')
Run Code Online (Sandbox Code Playgroud)

请注意,'miter'(方角)似乎是 MPL 1.4 中的默认值,因此在您的情况下实际上不需要指定此参数。我把它包括在这里是为了明确。