ylangylang
ylangylang

Reputation: 3484

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?

Upvotes: 13

Views: 25953

Answers (2)

OriolAbril
OriolAbril

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:

enter image description here

Upvotes: 24

ImportanceOfBeingErnest
ImportanceOfBeingErnest

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

Related Questions