Reputation: 3484
I would like to make histograms that are both hatched and filled (like these bar plots on the left in this matplotlib example):
Here's the code I tried to use:
import matplotlib.pyplot as plt
plt.hist(values, bins, histtype='step', linewidth=2, facecolor='c', hatch='/')
But no matter whether I specify "facecolor" or "color", only the lines of the hatching appear in colour and the histogram is still unfilled. How can I make the hatching show up on top of a filled histogram?
Upvotes: 13
Views: 25953
Reputation: 8803
In order to fill the area below the histogram the kwarg fill
can be set to True
. Then, the facecolor and edgecolor can be set in order to use different colors for the hatch and the background.
plt.hist(np.random.normal(size=500), bins=10, histtype='step', linewidth=2, facecolor='c',
hatch='/', edgecolor='k',fill=True)
This generates the following output:
Upvotes: 24
Reputation: 339430
histtype='step'
draws step lines. They are by definition not filled (because they are lines.
Instead, use histtype='bar'
(which is the default, so you may equally leave it out completely).
Upvotes: 6