matplotlib 孵化和填充直方图

rfe*_*and 10 python matplotlib python-2.7

我想制作阴影和填充的直方图(就像这个matplotlib 示例左侧的这些条形图):

孵化图

这是我尝试使用的代码:

import matplotlib.pyplot as plt
plt.hist(values, bins, histtype='step', linewidth=2, facecolor='c', hatch='/')
Run Code Online (Sandbox Code Playgroud)

但是无论我指定“facecolor”还是“color”,只有阴影线出现彩色,直方图仍然没有填充。如何使阴影显示在填充直方图的顶部?

Ori*_*ril 12

为了填充直方图下方的区域,fill可以将kwarg设置为True。然后,可以设置 facecolor 和 edgecolor,以便为影线和背景使用不同的颜色。

plt.hist(np.random.normal(size=500), bins=10, histtype='step', linewidth=2, facecolor='c', 
         hatch='/', edgecolor='k',fill=True)
Run Code Online (Sandbox Code Playgroud)

这将生成以下输出:

在此处输入图片说明


Imp*_*est 5

histtype='step'绘制阶梯线。根据定义,它们是未填充的(因为它们是线条。

相反,使用histtype='bar'(这是默认设置,因此您也可以完全忽略它)。