Insérer une image (photo) dans une figure matplotlib

Exemple de comment insérer une image extérieure dans une figure matplotlib en utilisant annotation. L'image utilisée ici est Lenna.png

Insérer une image dans une figure matplotlib
Insérer une image dans une figure matplotlib

from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

fig, ax = plt.subplots()

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)

arr_lena = mpimg.imread('Lenna.png')

imagebox = OffsetImage(arr_lena, zoom=0.2)

ab = AnnotationBbox(imagebox, (0.4, 0.6))

ax.add_artist(ab)

plt.grid()

plt.draw()
plt.savefig('add_picture_matplotlib_figure.png',bbox_inches='tight')
plt.show()

Références

Image

of