根據畫素值設定 Matplotlib 中的透明度


要根據 matplotlib 中的畫素值設定透明度,請獲取掩碼資料,資料值小於某個值時在該處獲取掩碼資料。值越小,兩幅影像之間的完全重疊程度越大。

步驟

  • 使用 numpy 建立 data1 data2 

  • 使用 numpy 的 masked_where() 方法獲取掩碼資料。

  • 使用 subplots() 方法建立圖形和一組子圖 (fig 和 ax)。

  • 使用 imshow() 方法,使用不同的配色圖(jet 和灰度)顯示資料(data1 掩碼資料)作為影像,即,在 2D 正則光柵上顯示。

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

示例

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
data1 = np.random.rand(50, 50)
data2 = np.random.rand(50, 50)
masked_data = np.ma.masked_where(data2 < .7, data2)
fig, ax = plt.subplots()
ax.imshow(data2, cmap=cm.gray)
ax.imshow(masked_data, cmap=cm.jet, interpolation='none')
plt.show()

輸出

更新於: 08-May-2021

947 次瀏覽

開啟你的 事業

完成課程即可獲得認證

開始
廣告