如何在 Matplotlib 中在 jpg 影像頂部直接疊加散點圖?


要直接在 jpg 影像頂部疊加散點圖,我們可以執行以下步驟 −

  • 使用 imread() 方法載入影像“bird.jpg”,將影像從檔案中讀入陣列中。

  • 現在將資料顯示為影像。

  • 要在影像上繪製散點,請為 x_points 和 y_points 建立列表。

  • 為 x 和 y 生成隨機數並追加到列表中。

  • 使用 scatter 方法繪製 x 和 y 點。

  • 要顯示圖形,請使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
data = plt.imread("logo2.jpg")
im = plt.imshow(data)
x_points = []
y_points = []
for i in range(10):
   x_points.append(np.random.randint(0, 700))
   y_points.append(np.random.randint(0, 700))
plt.scatter(x_points, y_points, c=x_points)
plt.show()

輸出

更新於: 06-May-2021

2K+ 瀏覽

啟動你的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.