hatch more than one data serie in a histogram

matplotlib, numpy, python

Solution

Unfortunately, it doesn't look like hist supports multiple hatches for multi-series plots. However, you can get around that with the following:

import numpy as np
import matplotlib.pylab as plt    

data = [np.random.rand(100) + 10 * i for i in range(3)]
ax1 = plt.subplot(111)

n, bins, patches = ax1.hist(data, 20, histtype='bar',
                        color=['0', '0.33', '0.66'],
                        label=['normal I', 'normal II', 'normal III'])

hatches = ['', 'o', '/']
for patch_set, hatch in zip(patches, hatches):
    for patch in patch_set.patches:
        patch.set_hatch(hatch)

The object `patches` returned by `hist` is a list of `BarContainer` objects, each of which holds a set of `Patch` objects (in `BarContainer.patches`). So you can access each patch object and set its hatch explicitly.

or as @MadPhysicist pointed out you can use `plt.setp` on each `patch_set` so the loop can be shortened to:

for patch_set, hatch in zip(patches, hatches):
    plt.setp(patch_set, hatch=hatch)

Problem

When I colored an histogram it accept a list for the different colors, however, for hatching it accept only one value. This is the code: ``` import numpy as np import matplotlib.pylab as plt data = [np.random.rand(100) + 10 * i for i in range(3)] ax1 = plt.subplot(111) n, bins, patches = ax1.hist(data, 20, histtype='bar', color=['0', '0.33', '0.66'], label=['normal I', 'normal II', 'normal III'], hatch= ['', 'o', '/']) ``` How can I have different hatch for the different series?

Original source