如何讓 Matplotlib 散點圖作為一個整體透明顯示?


要讓 matplotlib 散點圖作為一個整體透明顯示,我們可以在 scatter() 方法自變數中使用不同的組值來更改 alpha 值。

步驟

  • 設定圖形大小並調整各子圖之間和周圍的間距。

  • 編寫一個方法來返回一個分組的 x 和 y 點。

  • 獲取組 1 和組 2 的資料點。

  • 使用 scatter() 方法繪製組 1,x 和 y 點,方法是使用 color=green 和 alpha=0.5。

  • 使用 scatter() 方法繪製組 2,x 和 y 點,方法是使用 color=red 和 alpha=0.5。

  • 使用 show() 方法來顯示圖形。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
def get_group_points():
   return np.random.rand(100), np.random.rand(100),
group_1 = get_group_points()
group_2 = get_group_points()
plt.scatter(group_1[0], group_1[1], color='green', alpha=0.5, s=100)
plt.scatter(group_2[0], group_2[1], color='red', alpha=0.5, s=100)
plt.show()

輸出

更新時間: 2021 年 6 月 1 日

4K+ 瀏覽

開啟你的 職業生涯

完成課程,獲得認證

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