【发布时间】: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)
【问题讨论】:
-
您认为example 和
histtype='step'是圆的还是尖锐的? -
请提供(最少)数量的代码来重现您的问题。
-
是的,我在示例中看到了这一点。在上面的最小代码中,我夸大了线宽,以便效果更加明显。有什么想法吗?
-
@frixhax 为什么你甚至想增加你的线宽(我可以看到你这样做是故意的)?如果你有默认的线宽,你仍然可以清楚地看到你的直方图,并且可以理解你的数据分布。
标签: python matplotlib histogram