Matplotlib Example | Display an Image with Watermark

In this matplotlib example code, we will introduce how to diaplay an image with watermark for beginners.

Step 1: load python library

import numpy as np 
import matplotlib.cbook as cbook 
import matplotlib.image as image 
import matplotlib.pyplot as plt 

Step 2: load watermark image data

For example:

with cbook.get_sample_data('img.jpg') as image_file: 
    image = plt.imread(image_file)

In this example, we use plt.imread() to load and get an watermark image data.

Step 3: draw a sine function in matplotlib

For example:

fig, ax = plt.subplots() 

ax.plot(np.sin(10 * np.linspace(0, 1)), '-o', ms = 15,alpha = 0.6, mfc ='green') 

Step 4: display watermark image in matplotlib

Here is the example code:

fig.figimage(im, 5, 5, zorder = 3, alpha =.5) 

We use fig.figimage() to display an image, alpha parameter is very important. It make an image be an image background.

Matplotlib Example | Display an Image with Watermark