修正 Matplotlib 散點圖中的顏色
要修正 Matplotlib 中散點圖的顏色,我們可以按照以下步驟進行操作
使用 numpy 建立 xs 和 ys 隨機資料點。
使用十六進位制字母建立一組顏色,等於 ys 的長度。
使用顏色列表,利用 scatter() 方法繪製列表 xs 和 ys。
若要顯示圖形,使用 show() 方法。
示例
import random import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xs = np.random.rand(100) ys = np.random.rand(100) colors = ["#" + ''.join([random.choice('0123456789ABCDEF') for j in range(6)]) for i in range(len(xs))] plt.scatter(xs, ys, c=colors) plt.show()
產出
廣告