如何使用 Matplotlib 在图的角落插入小图像?

matplotlibserver side programmingprogramming更新于 2025/4/30 11:07:17

要使用 Matplotlib 在图的角落插入小图像,我们可以采取以下步骤−

  • 使用 imread() 方法将文件中的图像读入数组。
  • 使用 subplots() 方法创建一个图形并添加一组子图。
  • 在当前轴上绘制一条线。
  • 创建新轴 (newax) 以显示图像数组(步骤 1)。
  • 关闭为插入图像而创建的新轴。
  • 要显示图形,请使用 show() 方法。

示例

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()

输出


相关文章