我使用 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)
最佳答案
控制此属性的功能在 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')
请注意,'miter'
(方角)似乎是 MPL 1.4 中的默认值,因此在您的情况下实际上不需要指定此参数。为了明确起见,我将其包含在此处。
关于python - matplotlib 直方图中的圆条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486944/