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?
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:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With