How to insert a small image on the corner of a plot with Matplotlib?

MatplotlibServer Side ProgrammingProgramming

To insert a small image on the corner of a plot with matplotlib, we can take the following steps−

  • Read an image from a file into an array using imread() method.
  • Using subplots() method, create a figure and add a set of subplots.
  • Plot a line on the current axis.
  • Create newax (new axis) to show the image array (Step 1).
  • Turn off the newly created axis, created for an image insert.
  • To display the figure, use show() method.

Example

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
im = plt.imread('bird.jpg') # insert local path of the image.
fig, ax = plt.subplots()
ax.plot(range(10))
newax = fig.add_axes([0.8,0.8,0.2,0.2], anchor='NE', zorder=1)
newax.imshow(im)
newax.axis('off')
plt.show()

Output

raja
Published on 07-May-2021 07:57:53
Advertisements