Hatch Patterns on Bar Charts and Fills in Matplotlib

Add stripes, crosses, and dots to bars, areas, and pie wedges with the hatch argument, and control the pattern's color, density, and line width.

To add a pattern to a bar in Matplotlib, pass hatch= with a pattern string, such as ax.bar(x, y, hatch="//"). The same argument works for fill_between(), pie wedges, and other filled shapes. Patterns keep series apart in black and white printing and for readers who can't tell the colors apart.

Quick Example

Grouped bars with a different color and pattern for each series:

import matplotlib.pyplot as plt
import numpy as np

regions = ["North", "South", "East", "West"]
sales = {
    "Online": [44, 38, 52, 47],
    "Retail": [31, 45, 28, 36],
    "Wholesale": [18, 22, 25, 14],
}
colors = ["#bde0fe", "#ffc8dd", "#d8f3dc"]
patterns = ["//", "..", "xx"]

x = np.arange(len(regions))
width = 0.26

fig, ax = plt.subplots(figsize=(8, 4.5), layout="constrained")
for i, ((channel, values), color, hatch) in enumerate(
        zip(sales.items(), colors, patterns)):
    ax.bar(x + i * width, values, width, label=channel,
           color=color, hatch=hatch, edgecolor="#333333")

ax.set_xticks(x + width, regions)
ax.set_ylabel("Sales ($ thousands)")
ax.legend(ncols=3, handleheight=1.8, handlelength=3)
ax.set_ylim(0, 62)
plt.show()

Grouped bar chart of sales by region with diagonal stripes for online, dots for retail and crosses for wholesale

handleheight and handlelength enlarge the legend swatches so the patterns are recognizable. See grouped bar charts for how the bar positions work.

All Matplotlib Hatch Patterns

There are ten base characters. Combine them to layer patterns.

base = ["/", "\\", "|", "-", "+", "x", "o", "O", ".", "*"]
combined = ["//", "////", "/o", "\\|", "-\\", "++", "xo", "..", "O.", "**"]

fig, axs = plt.subplots(2, 10, figsize=(10, 3.2), layout="constrained")
for ax, hatch in zip(axs.flat, base + combined):
    ax.bar(0, 1, width=1, color="white", edgecolor="black", hatch=hatch)
    ax.set_title(repr(hatch), fontfamily="monospace", fontsize=11)
    ax.margins(0)
    ax.axis("off")
plt.show()

Twenty swatches showing each Matplotlib hatch character and some combinations

The titles show Python's repr(), so each backslash appears doubled. In code, hatch="\\" is a single backslash.

Control Hatch Density

Repeat a character to pack the pattern more tightly: "/" is sparse, "//" denser, and "////" denser still.

Set the Hatch Color and Line Width

The hatch takes the bar's edgecolor by default. Matplotlib 3.11 added hatchcolor to set it separately from the edge, and 3.10 added hatch_linewidth:

months = ["Jan", "Feb", "Mar", "Apr"]
values = [12, 17, 14, 21]

fig, axs = plt.subplots(1, 3, figsize=(10, 3.4), sharey=True, layout="constrained")

axs[0].bar(months, values, color="#fefae0", hatch="//", edgecolor="#bc6c25")
axs[0].set_title("edgecolor sets both")

axs[1].bar(months, values, color="#fefae0", hatch="//",
           edgecolor="black", hatchcolor="#dda15e")
axs[1].set_title("separate hatchcolor")

axs[2].bar(months, values, color="#283618", hatch="//",
           hatchcolor="white", linewidth=0, hatch_linewidth=3)
axs[2].set_title("thick white hatch")
plt.show()

Three bar charts: brown edges and stripes, black edges with tan stripes, and dark bars with thick white stripes

On Matplotlib 3.10 and older, use edgecolor for the pattern color. If you don't set an edge color there, the hatch uses plt.rcParams["hatch.color"], which defaults to black. Line width for all hatches is plt.rcParams["hatch.linewidth"].

Hatch Individual Bars

ax.bar() accepts a list with one pattern per bar. To mark specific bars after plotting, call set_hatch() on them. Here the forecast months are striped:

months = ["Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
revenue = [61, 64, 70, 74, 79, 92]

fig, ax = plt.subplots(figsize=(8, 4), layout="constrained")
bars = ax.bar(months, revenue, color="#457b9d", edgecolor="white")

for bar in bars[3:]:
    bar.set_facecolor("#a8dadc")
    bar.set_hatch("///")

ax.legend([bars[0], bars[3]], ["Actual", "Forecast"])
ax.set_ylabel("Revenue ($ thousands)")
plt.show()

Monthly revenue bars with July to September solid and October to December striped as forecast

The same loop works on bars drawn by pandas or Seaborn: iterate over ax.containers.

Hatch an Area With fill_between()

Use facecolor="none" to draw only the pattern, which lets lines and gridlines show through:

t = np.linspace(0, 10, 200)
expected = 20 + 3 * t
low, high = expected - 2 - t, expected + 2 + t

fig, ax = plt.subplots(figsize=(8, 4), layout="constrained")
ax.plot(t, expected, color="black", label="Expected")
ax.fill_between(t, low, high, facecolor="none", hatch="..",
                edgecolor="#6d597a", linewidth=1, label="Likely range")
ax.fill_between(t, high, 75, facecolor="#fde2e4", hatch="\\\\",
                edgecolor="#e56b6f", linewidth=0, label="Over capacity")
ax.set_xlim(0, 10)
ax.set_ylim(0, 75)
ax.legend(loc="upper left", handleheight=1.8, handlelength=3)
plt.show()

Line chart with a dotted band for the likely range and a striped red area above it marked over capacity

Hatch Pie Chart Wedges

ax.pie() takes a list of patterns (Matplotlib 3.7+) and cycles through it:

fig, ax = plt.subplots(figsize=(6, 4.5), layout="constrained")
ax.pie([45, 30, 15, 10], labels=["Search", "Direct", "Social", "Email"],
       colors=["#f4f1de", "#f2cc8f", "#81b29a", "#e07a5f"],
       hatch=["//", "..", "\\\\", "++"],
       wedgeprops={"edgecolor": "#3d405b"})
plt.show()

Pie chart of traffic sources with a different pattern in each of four wedges

Practical Tips

  • Two to four patterns are readable. Past that, label the series directly.
  • Pair patterns with light fills so the lines stand out.
  • Vector output (SVG, PDF) keeps patterns sharp when printed. See saving figures.

Similar Topics