Python Matplotlib Pie Chart Hatch

In my years of working as a Python data analyst, I have often found that colors alone aren’t enough to make a chart stand out.

Sometimes, you need to differentiate data slices for black-and-white printing or to assist users with color vision deficiencies.

Adding patterns, or “hatching,” to your Python Matplotlib pie charts is the professional way to handle these scenarios.

In this tutorial, I will show you exactly how to implement the Python Matplotlib pie chart hatch feature to create stunning, accessible visualizations.

The Basic Python Matplotlib Pie Chart Hatch Syntax

When I first started using Python for data visualization, I struggled to find a direct “hatch” parameter within the pie function.

Unlike bar charts, the plt.pie() function in Python does not have a global hatch argument that applies to all slices automatically.

Instead, we have to manipulate the “patches” (the individual wedges) after the Python Matplotlib pie chart is created.

To start, let’s look at how to apply a single hatch pattern to all slices in a Python script.

import matplotlib.pyplot as plt

# Data representing USA tech market share (Example)
labels = ['Apple', 'Microsoft', 'Google', 'Amazon']
sizes = [35, 30, 20, 15]

# Create the pie chart
fig, ax = plt.subplots()
patches, texts, autotexts = ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)

# Applying hatch to each patch (wedge)
for patch in patches:
    patch.set_hatch('/')

plt.title("USA Tech Giants Market Share (Single Hatch Python Example)")
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Pie Chart Hatch

The set_hatch() method is the key here, allowing us to define the pattern for the Python object.

Use Different Hatch Patterns for Each Python Pie Slice

In a real-world Python project, you usually want each slice to have a unique identity. I often use this technique when I am preparing reports for stakeholders who might print documents in grayscale.

Python Matplotlib supports several hatch symbols like /, \, |, -, +, x, o, O, ., and *.

Here is how I assign a unique Python hatch pattern to each slice representing different industries in the US economy.

import matplotlib.pyplot as plt

# USA Economic Sector Data
sectors = ['Healthcare', 'Finance', 'Tech', 'Retail', 'Energy']
shares = [25, 20, 25, 15, 15]
# Define a list of patterns for our Python chart
patterns = ['/', 'o', 'x', '\\', '*']

fig, ax = plt.subplots(figsize=(8, 8))
patches, texts, pcts = ax.pie(shares, labels=sectors, autopct='%1.1f%%', 
                              startangle=140, colors=['white']*5, 
                              wedgeprops={'edgecolor': 'black'})

# Mapping patterns to patches in Python
for i in range(len(patches)):
    patches[i].set_hatch(patterns[i])

plt.title("Composition of US Economic Sectors (Patterned Python Chart)")
plt.tight_layout()
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Pie Chart Hatch in Python

In this Python example, I set the colors to white to emphasize the hatch patterns.

Control Python Hatch Density and Color

For instance, in Python Matplotlib, // is denser than /, and /// is denser still. Furthermore, the color of the hatch is controlled by the edgecolor of the patch in Python.

If you want the lines of the hatch to be a specific color, you must set the edge color of that specific Python wedge.

import matplotlib.pyplot as plt

# US Population by Age Group (Mock Data)
age_groups = ['0-18', '19-35', '36-50', '51-70', '71+']
population_dist = [22, 25, 20, 23, 10]
# Denser patterns for Python visualization
dense_patterns = ['...', '///', '---', '+++', '***']

fig, ax = plt.subplots(figsize=(10, 7))
patches, texts, pcts = ax.pie(population_dist, labels=age_groups, 
                              autopct='%1.1f%%', startangle=90)

# Customizing density and color for each Python slice
for patch, pattern in zip(patches, dense_patterns):
    patch.set_hatch(pattern)
    patch.set_edgecolor('white') # Makes the hatch lines white

plt.title("US Age Demographics with Custom Python Hatch Density")
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Pie Chart Hatch Python

By setting the edgecolor to white in this Python script, I created a high-contrast look that is very popular in modern Python dashboarding.

I find that using zip() in Python is the cleanest way to pair your patches with your desired patterns.

Combine Python Pie Chart Hatch with Exploding Slices

Sometimes you want to highlight a specific part of the American market, such as the “Electric Vehicle” segment within the broader auto industry.

In Python Matplotlib, we can “explode” a slice and still maintain the hatch integrity.

I use this approach when I want the viewer’s eye to go straight to a specific data point in my Python report.

import matplotlib.pyplot as plt

# USA Auto Market Share
labels = ['Gasoline', 'Hybrid', 'Electric', 'Diesel']
sizes = [60, 20, 15, 5]
explode = (0, 0, 0.1, 0) # Explode the 'Electric' slice
patterns = ['', '', 'OO', ''] # Only hatch the Electric slice

fig, ax = plt.subplots()
patches, texts, pcts = ax.pie(sizes, explode=explode, labels=labels, 
                              autopct='%1.1f%%', shadow=True, startangle=90)

# Highlight the exploded slice with a Python hatch
for patch, pattern in zip(patches, patterns):
    patch.set_hatch(pattern)

plt.title("USA Auto Market Trend: Focus on Electric (Python Visualization)")
plt.show()

In this Python code, notice how I left some patterns empty (”). This is a great Python technique to use when you want only one specific slice to have a texture, making it the focal point of the entire Python chart.

Customize Python Hatch with Object-Oriented Style

As you become a more advanced Python developer, you will likely prefer the object-oriented approach over plt.pie.

Using the ax object in Python allows for better subplot management and cleaner code for large-scale Python applications.

I always recommend this method if you are building a Python library or a complex data tool.

import matplotlib.pyplot as plt

# US Energy Production Sources
sources = ['Natural Gas', 'Renewables', 'Nuclear', 'Coal']
data = [40, 20, 20, 20]
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']

fig, ax = plt.subplots()
wedges, texts, autotexts = ax.pie(data, labels=sources, colors=colors, 
                                  autopct='%1.1f%%', startangle=40)

# Object-oriented Python hatch application
hatch_styles = ['/', '\\', '|', '-']
for i, wedge in enumerate(wedges):
    wedge.set_hatch(hatch_styles[i])

# Adding a circle at the center to make it a donut chart
centre_circle = plt.Circle((0,0),0.70,fc='white')
fig.gca().add_artist(centre_circle)

ax.set_title("US Energy Mix (Python Donut Chart with Hatching)")
plt.axis('equal') 
plt.tight_layout()
plt.show()

Transforming a Python pie chart into a donut chart while keeping the hatches is a great way to add a modern touch to your Python project.

I personally love how the Python hatches interact with the white center of the donut, creating a very clean aesthetic.

Troubleshoot Common Python Matplotlib Hatch Issues

One thing I noticed early on is that Python hatches can sometimes look “pixelated” in the default Matplotlib viewer.

If you are exporting your Python charts for a US-based publication, I highly recommend saving them in a vector format like PDF or SVG.

When you use plt.savefig(‘chart.pdf’) in your Python script, the hatch patterns remain crisp and infinitely scalable.

Conclusion

Adding a hatch to a Python Matplotlib pie chart is a simple yet powerful way to upgrade your data visualizations.

It ensures that your Python charts are accessible, professional, and clear, even without color. I’ve found that these small Python styling choices often make the difference between a mediocre report and a high-quality data story.

Other Python Tutorials You Might Like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.