How to decrease the hatch density in Matplotlib?

In Matplotlib, hatch patterns have a default density that might appear too dense for certain visualizations. You can decrease hatch density by creating a custom hatch class that overrides the default density behavior.

Understanding Hatch Density

Hatch density refers to how closely packed the hatch lines or patterns appear in a plot. Lower density means more spacing between pattern elements, while higher density creates tighter patterns.

Creating a Custom Hatch Class

To control hatch density, we need to create a custom hatch class that inherits from Matplotlib's built-in hatch classes ?

import matplotlib.pyplot as plt
from matplotlib import hatch

# Set figure parameters
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

class MyHorizontalHatch(hatch.HorizontalHatch):
    def __init__(self, hatch, density):
        char_count = hatch.count('o')
        if char_count > 0:
            # Reduce density by dividing by char_count
            self.num_lines = int((1.0 / char_count) * density)
        else:
            self.num_lines = 0
        self.num_vertices = self.num_lines * 2
        super().__init__(hatch, density)

# Register the custom hatch class
hatch._hatch_types.append(MyHorizontalHatch)

# Create the plot
fig = plt.figure()
ax1 = fig.add_subplot(111)

x = [1, 2, 3]
y = [4, 6, 3]

ax1.bar(x, y, color='lightblue', edgecolor='navy', hatch="o", linewidth=1.0)

plt.title("Bar Chart with Custom Hatch Density")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()

How the Custom Hatch Works

The custom MyHorizontalHatch class works by ?

  • Counting pattern characters: Uses hatch.count('o') to determine pattern complexity

  • Calculating line density: Reduces num_lines by dividing the base density

  • Setting vertices: Calculates num_vertices based on the reduced line count

Comparison Example

Here's a comparison showing default vs. reduced hatch density ?

import matplotlib.pyplot as plt
from matplotlib import hatch

class LowDensityHatch(hatch.HorizontalHatch):
    def __init__(self, hatch, density):
        char_count = hatch.count('o')
        if char_count > 0:
            # Further reduce density for more spacing
            self.num_lines = max(1, int((0.3 / char_count) * density))
        else:
            self.num_lines = 0
        self.num_vertices = self.num_lines * 2
        super().__init__(hatch, density)

hatch._hatch_types.append(LowDensityHatch)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

# Default density (without custom class)
x = [1, 2]
y = [3, 4]
ax1.bar(x, y, color='lightgreen', hatch='///', alpha=0.7)
ax1.set_title("Default Hatch Density")

# Custom low density
ax2.bar(x, y, color='lightcoral', hatch='o', alpha=0.7)
ax2.set_title("Reduced Hatch Density")

plt.tight_layout()
plt.show()

Key Parameters

Parameter Purpose Effect on Density
char_count Count of pattern characters Higher count = lower density
num_lines Number of hatch lines Fewer lines = lower density
density Base density value Multiplier for line calculation

Conclusion

Custom hatch classes allow precise control over pattern density in Matplotlib plots. By overriding the num_lines calculation, you can create more spaced-out hatch patterns that improve visual clarity and aesthetics.

Updated on: 2026-03-26T00:27:33+05:30

885 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements