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 plot a watermark image in Matplotlib?
A watermark is a semi-transparent image overlay that appears on top of your plot. Matplotlib provides the figimage() method to add watermark images directly to figures.
Basic Watermark Example
Here's how to add a watermark image to a matplotlib plot ?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cbook
import matplotlib.image as image
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Load sample watermark image
with cbook.get_sample_data('logo2.png') as file:
watermark = image.imread(file)
# Create plot
fig, ax = plt.subplots()
# Plot main data
x = np.linspace(0, 1, 10)
y = np.sin(10 * x)
ax.plot(y, '-o', ms=20, alpha=0.7, mfc='orange')
# Add watermark image
fig.figimage(watermark, 10, 10, zorder=3, alpha=0.5)
plt.title('Plot with Watermark')
plt.show()
Key Parameters
The figimage() method accepts several important parameters ?
| Parameter | Description | Example |
|---|---|---|
X |
Array of image data | watermark |
xo, yo |
Position coordinates | 10, 10 |
alpha |
Transparency (0-1) | 0.5 |
zorder |
Layer order | 3 |
Custom Watermark Positioning
You can position the watermark at different locations by adjusting coordinates ?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cbook
import matplotlib.image as image
# Create sample data
x = np.linspace(0, 10, 100)
y = np.cos(x) * np.exp(-x/5)
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(x, y, 'b-', linewidth=2)
ax.set_title('Exponential Decay with Watermark')
# Load watermark
with cbook.get_sample_data('logo2.png') as file:
watermark = image.imread(file)
# Position watermark in top-right corner
fig_width = fig.get_figwidth() * fig.dpi
fig_height = fig.get_figheight() * fig.dpi
watermark_width = watermark.shape[1]
# Calculate position for top-right
x_pos = fig_width - watermark_width - 20
y_pos = fig_height - watermark.shape[0] - 20
fig.figimage(watermark, x_pos, y_pos, alpha=0.3, zorder=10)
plt.show()
Multiple Watermarks
You can add multiple watermarks at different positions and transparency levels ?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cbook
import matplotlib.image as image
# Create sample plot
fig, ax = plt.subplots(figsize=(10, 6))
# Sample data
data = np.random.randn(1000)
ax.hist(data, bins=30, alpha=0.7, color='skyblue')
ax.set_title('Histogram with Multiple Watermarks')
# Load watermark
with cbook.get_sample_data('logo2.png') as file:
watermark = image.imread(file)
# Add watermarks at different positions
fig.figimage(watermark, 50, 50, alpha=0.2, zorder=1) # Bottom-left
fig.figimage(watermark, 400, 300, alpha=0.15, zorder=1) # Center
fig.figimage(watermark, 650, 450, alpha=0.25, zorder=1) # Top-right
plt.tight_layout()
plt.show()
Watermark Best Practices
- Alpha transparency: Use values between 0.1-0.5 to avoid obscuring data
- Positioning: Place in corners or areas with minimal data
- Size consideration: Ensure watermark doesn't dominate the plot
- Z-order: Use appropriate layering to control visibility
Conclusion
Use figimage() to add watermarks to matplotlib plots. Control transparency with alpha and position with coordinate parameters. Multiple watermarks can be layered using different z-order values.
Advertisements
