简体   繁体   English

matplotlib直方图中的圆条

[英]Round bars in matplotlib histogram

I use matplotlib.pyplot.hist() for a histogram plot in python. 我将matplotlib.pyplot.hist()用于python中的直方图。 If I use a visible edgewidth, for example when using histtype='step' , the upper corners of the single bars are slightly round. 如果我使用可见的边宽,例如,当使用histtype='step' ,单个条的上角会稍微变圆。 I want them to be sharply rectangular instead. 我希望它们改为锐利的矩形。 I already tried using the solid_capstyle keyword, which works for influencing the shape of line plots, but this doesn't work in hist() . 我已经尝试过使用solid_capstyle关键字,该关键字可影响线形图的形状,但这在hist()不起作用。 Any ideas on how to do that? 关于如何做到这一点的任何想法? Thanks! 谢谢!

Here's my minimal self-contained example 这是我最小的独立示例

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)

The capability to control this property is available in MatPlotLib 1.4. MatPlotLib 1.4提供了控制此属性的功能。 So, I recommend upgrading; 因此,我建议升级; for example if you use pip: 例如,如果您使用pip:

pip install --upgrade matplotlib

Then use the joinstyle keyword argument ( ['miter' | 'round' | 'bevel'] ) in your calls to hist ; 然后在调用hist使用joinstyle关键字参数( ['miter' | 'round' | 'bevel'] ); eg: 例如:

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

Note that 'miter' (square corners) appears to be the default in MPL 1.4, so specifying this parameter is not actually necessary in your case. 请注意,MPL 1.4中默认使用'miter' (方角),因此在您的情况下实际上不需要指定此参数。 I've included it here to be explicit. 我将其包括在内是为了明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM