【问题标题】:Round bars in matplotlib histogrammatplotlib 直方图中的圆条
【发布时间】:2014-10-21 12:37:54
【问题描述】:

我在 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)

【问题讨论】:

  • 您认为examplehisttype='step' 是圆的还是尖锐的?
  • 请提供(最少)数量的代码来重现您的问题。
  • 是的,我在示例中看到了这一点。在上面的最小代码中,我夸大了线宽,以便效果更加明显。有什么想法吗?
  • @frixhax 为什么你甚至想增加你的线宽(我可以看到你这样做是故意的)?如果你有默认的线宽,你仍然可以清楚地看到你的直方图,并且可以理解你的数据分布。

标签: python matplotlib histogram


【解决方案1】:

在 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 中的默认值,因此在您的情况下实际上不需要指定此参数。为了明确起见,我将其包含在此处。

【讨论】:

  • 谢谢!是的,它现在似乎默认了。
猜你喜欢
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 2022-01-21
  • 2011-07-16
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多