Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How can I add textures to my bars and wedges in Matplotlib?
Matplotlib provides the hatch parameter to add texture patterns to bars and wedges. This enhances visual distinction between different data categories and creates more engaging visualizations.
Adding Textures to Bar Charts
The hatch parameter accepts various pattern strings to create different textures ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [8, 5]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
# Different hatch patterns
textures = ["//", "*", "o", "d", ".", "++", "xx", "||"]
# Create bars with different textures
for i in range(len(textures)):
height = np.random.randint(3, 8)
ax.bar(i, height, color="lightblue", edgecolor="black",
alpha=0.7, hatch=textures[i], label=f'Pattern {textures[i]}')
ax.set_xlabel('Bar Index')
ax.set_ylabel('Value')
ax.set_title('Bar Chart with Different Textures')
plt.show()
Adding Textures to Pie Chart Wedges
Wedges in pie charts can also use hatch patterns for better visual separation ?
import matplotlib.pyplot as plt
# Data for pie chart
sizes = [25, 30, 20, 25]
labels = ['A', 'B', 'C', 'D']
hatches = ['///', '***', '...', '+++']
colors = ['lightcoral', 'lightskyblue', 'lightgreen', 'gold']
plt.figure(figsize=(8, 6))
wedges, texts, autotexts = plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', startangle=90,
wedgeprops=dict(edgecolor='black'))
# Apply different hatches to each wedge
for wedge, hatch in zip(wedges, hatches):
wedge.set_hatch(hatch)
plt.title('Pie Chart with Textured Wedges')
plt.show()
Available Hatch Patterns
| Pattern | Description | Symbol |
|---|---|---|
/ |
Diagonal lines (right) | Single forward slash |
\ |
Diagonal lines (left) | Single backslash |
| |
Vertical lines | Pipe symbol |
- |
Horizontal lines | Dash symbol |
+ |
Plus signs | Plus symbol |
x |
X marks | Letter x |
o |
Small circles | Letter o |
* |
Stars | Asterisk |
Combining Multiple Patterns
You can combine patterns by repeating characters for denser textures ?
import matplotlib.pyplot as plt
import numpy as np
categories = ['Category A', 'Category B', 'Category C']
values = [15, 25, 20]
# Dense patterns using repeated characters
dense_hatches = ['///', '***', '+++']
plt.figure(figsize=(8, 5))
bars = plt.bar(categories, values, color=['red', 'green', 'blue'],
alpha=0.6, edgecolor='black', linewidth=2)
# Apply dense hatches
for bar, hatch in zip(bars, dense_hatches):
bar.set_hatch(hatch)
plt.title('Bar Chart with Dense Texture Patterns')
plt.ylabel('Values')
plt.show()
Conclusion
Use the hatch parameter in bar() and set_hatch() for wedges to add texture patterns. Combine multiple pattern characters for denser textures and better visual distinction between data categories.
Advertisements
