Controlling a fill pattern
matplotlib offers fairly limited support to fill surfaces with a pattern. For line patterns, it can be helpful when preparing figures for black-and-white prints. In this recipe, we are going to look at how we can fill surfaces with a pattern.
How to do it...
Let's demonstrate the use of fill patterns with a bar chart as follows:
import numpy as np import matplotlib.pyplot as plt N = 8 A = np.random.random(N) B = np.random.random(N) X = np.arange(N) plt.bar(X, A, color = 'w', hatch = 'x') plt.bar(X, A + B, bottom = A, color = 'w', hatch = '/') plt.show()
The preceding script produces the following graph:
How it works...
Rendering function filling volumes, such as pyplot.bar()
, accept an optional parameter, hatch
. This parameter can take the following values:
/
\
|
-
+
x
o
O
.
*
Each value corresponds to a different hatching pattern. The color
parameter will control the background color of the pattern, while the edgecolor
parameter will control the color of the hatching.