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 to change the linewidth of a hatch in Matplotlib?
To change the linewidth of a hatch in matplotlib, we can set the linewidth of the hatch using plt.rcParams['hatch.linewidth']. This parameter controls how thick or thin the hatch lines appear in your plots.
Steps
- Set the figure size and adjust the padding between and around the subplots
- Create x and y=sin(x) data points using numpy
- Set the linewidth of the hatch using
plt.rcParams['hatch.linewidth'] - Plot x and y data points using scatter() method with a square marker having "/" hatches with set linewidth
- Display the figure using show() method
Example
Here's how to create a scatter plot with custom hatch linewidth ?
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-5, 5, 25)
y = np.sin(x)
# Set the linewidth of the hatch
plt.rcParams['hatch.linewidth'] = 1
plt.scatter(x, y, s=700, marker='s', linewidth=0.05, facecolor='red',
hatch='/', alpha=.7)
plt.show()
Comparison of Different Hatch Linewidths
Let's compare different hatch linewidth values to see the visual difference ?
import numpy as np
from matplotlib import pyplot as plt
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
x = np.linspace(-2, 2, 10)
y = np.sin(x)
linewidths = [0.5, 2.0, 4.0]
titles = ['Thin (0.5)', 'Medium (2.0)', 'Thick (4.0)']
for i, (lw, title) in enumerate(zip(linewidths, titles)):
plt.rcParams['hatch.linewidth'] = lw
axes[i].scatter(x, y, s=500, marker='s', facecolor='lightblue',
hatch='///', alpha=0.7, edgecolor='black')
axes[i].set_title(f'Hatch Linewidth: {title}')
axes[i].grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Different Hatch Patterns with Custom Linewidth
You can also combine different hatch patterns with custom linewidths ?
import numpy as np
from matplotlib import pyplot as plt
fig, ax = plt.subplots(figsize=(10, 6))
# Set custom hatch linewidth
plt.rcParams['hatch.linewidth'] = 2.5
patterns = ['/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*']
colors = ['red', 'blue', 'green', 'orange', 'purple', 'brown', 'pink', 'gray', 'cyan', 'yellow']
for i, (pattern, color) in enumerate(zip(patterns, colors)):
ax.bar(i, 1, hatch=pattern, facecolor=color, alpha=0.6, edgecolor='black')
ax.set_xlabel('Hatch Pattern Index')
ax.set_ylabel('Height')
ax.set_title('Different Hatch Patterns with Linewidth = 2.5')
ax.set_xticks(range(len(patterns)))
ax.set_xticklabels(patterns)
plt.tight_layout()
plt.show()
Key Points
| Parameter | Purpose | Default Value |
|---|---|---|
hatch.linewidth |
Controls thickness of hatch lines | 1.0 |
hatch |
Defines the hatch pattern | None |
alpha |
Controls transparency | 1.0 (opaque) |
Conclusion
Use plt.rcParams['hatch.linewidth'] to control the thickness of hatch lines in matplotlib plots. Higher values create thicker hatch lines, while lower values create thinner, more subtle patterns.
Advertisements
