python - matplotlib 直方图中的圆条

标签 python matplotlib histogram

我使用 matplotlib.pyplot.hist() 在 python 中绘制直方图。如果我使用可见的边宽,例如当使用 histt​​ype='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/

相关文章:

Python 和 Matplotlib : characters as the x axis

python - 属性错误 : 'NoneType' object has no attribute 'dpi_scale_trans'

python - Python 中的 3D 图

java - 如何将 Java Hashmap 保存为 sql 表中的属性并将其加载回来

python - 如何在 python 中读取大型 .jl 文件

python - MongoDB、MongoEngine : unique list items

python - 如何显示每个 matplotlib 子图的 x 轴标签

r - 使用 R 中的 multhist 创建具有多个数据系列的直方图

python - 使用 Matplotlib 在绘图上叠加旋转图像

python - pandas.concat 两个数据框(一个有标题,一个没有标题)