如何避免 matplotlib.pyplot 中顏色的重複?


要避免 matplotlib.pyplot 中顏色的重複,我們可以採取以下步驟 −

步驟

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

  • 使用 numpy 建立xy 資料點。

  • 使用 plot() 方法繪製xy 資料點。

  • plot() 方法中,對color 屬性使用唯一十六進位制值,例如 color="#980ab5" 設定唯一顏色的圖形。您還可以指定您選擇的特定顏色,例如 color="green"

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

示例

import numpy as np
from matplotlib import pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# x and y data points
x = np.linspace(1, 100, 1000)
y = np.log(x)

# Plot the x and y data points with color attribute
plt.plot(x, y, color="#980ab5", lw=3)

# Display the plot
plt.show()

輸出

它將產生如下輸出 −

如果您在沒有 color 屬性的情況下執行上述程式碼,則圖形將採用預設顏色。

更新於: 01-Feb-2022

945 次瀏覽

開啟你的 職業生涯

完成課程認證

開始
廣告
© . All rights reserved.