如何根據 Matplotlib 中的某個變數更改資料點的顏色?


若要根據 matplotlib 中的某個變數更改資料點的顏色,我們可以執行以下步驟:-

  • 使用 numpy 建立 x、y 和 c 變數。

  • 使用 x、y 繪製散點圖,並對顏色使用 c(步驟 1)。

  • 要顯示影像,請使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 20, 50)
y = np.log(x)
c = np.random.randint(x)
plt.scatter(x, y, c=c)
plt.show()

輸出

更新於: 09-4-2021

1K+ 次瀏覽

開啟您的 職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.