如何使用 Matplotlib 在繪圖角插入一張小影像?


若要使用 matplotlib 在繪圖角插入一張小影像,我們可以執行以下步驟:-

  • 使用 imread() 方法從檔案中讀取一張圖片到陣列中。
  • 使用 subplots() 方法建立一個圖形並新增一組子圖。
  • 在當前座標軸上繪製一條線。
  • 建立新座標軸,用來顯示影像陣列(步驟 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()

輸出

更新時間:2021 年 5 月 7 日

7K+ 瀏覽量

開啟您的 職業生涯

完成課程後獲得認證

開始學習
廣告
© . All rights reserved.