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 do I fill a region with only hatch (no background colour) in matplotlib 2.0?
To fill a region with only hatch patterns and no background color in matplotlib, you need to set specific parameters in the fill_between() function. The key is using facecolor="none" to remove the background fill while keeping the hatch pattern visible.
Basic Syntax
The essential parameters for hatching without background color are ?
ax.fill_between(x, y, facecolor="none", hatch="pattern", edgecolor="color")
Complete Example
Here's how to create a hatched region with no background fill ?
import numpy as np import matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Number of sample data n = 256 # x and y data points x = np.linspace(-np.pi, np.pi, n, endpoint=True) y = np.sin(2 * x) # Create figure and subplot fig, ax = plt.subplots() # Plot the data line ax.plot(x, y, color='blue', alpha=1.0) # Fill area with hatch only (no background color) ax.fill_between(x, y, facecolor="none", hatch="o", edgecolor="blue", linewidth=1.0) # Display the plot plt.show()
Different Hatch Patterns
Matplotlib supports various hatch patterns that you can use ?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4, 100)
y1 = np.sin(x)
y2 = np.cos(x)
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
fig.suptitle('Different Hatch Patterns (No Background)')
# Diagonal lines pattern
axes[0,0].fill_between(x, y1, facecolor="none", hatch="///", edgecolor="red")
axes[0,0].plot(x, y1, color='red')
axes[0,0].set_title('Diagonal Lines (///)')
# Dots pattern
axes[0,1].fill_between(x, y2, facecolor="none", hatch="...", edgecolor="green")
axes[0,1].plot(x, y2, color='green')
axes[0,1].set_title('Dots (...)')
# Cross pattern
axes[1,0].fill_between(x, y1, facecolor="none", hatch="xxx", edgecolor="orange")
axes[1,0].plot(x, y1, color='orange')
axes[1,0].set_title('Crosses (xxx)')
# Stars pattern
axes[1,1].fill_between(x, y2, facecolor="none", hatch="***", edgecolor="purple")
axes[1,1].plot(x, y2, color='purple')
axes[1,1].set_title('Stars (***)')
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Purpose | Example Value |
|---|---|---|
facecolor |
Remove background fill | "none" |
hatch |
Define pattern type | "///" or "ooo" |
edgecolor |
Set hatch color | "blue" |
linewidth |
Control hatch thickness | 1.0 |
Common Hatch Patterns
- / - Forward diagonal lines
- \ - Backward diagonal lines
- | - Vertical lines
- - - Horizontal lines
- + - Crosses
- x - X pattern
- o - Circles
- O - Large circles
- . - Dots
- * - Stars
Conclusion
Use facecolor="none" with hatch parameter to create patterns without background fill. The edgecolor controls the hatch color, while linewidth adjusts the pattern thickness for better visibility.
Advertisements
