Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib hatched and filled histograms

I would like to make histograms that are both hatched and filled (like these bar plots on the left in this matplotlib example):

hatched_plot

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?

like image 697
ylangylang Avatar asked May 03 '18 17:05

ylangylang


2 Answers

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:

enter image description here

like image 107
OriolAbril Avatar answered Sep 27 '22 20:09

OriolAbril


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).

like image 40
ImportanceOfBeingErnest Avatar answered Sep 27 '22 20:09

ImportanceOfBeingErnest