如何在 matplotlib 圖表中迴圈遍歷顏色和線型?


要迴圈遍歷 matplotlib 圖表中的顏色和線型,我們可以採取以下步驟。

步驟

  • 設定圖表大小,並調整子圖表之間、周圍的間距。

  • 使用顏色線型,設定當前的rcParams

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

  • 要顯示圖表,請使用 Show() 方法。

示例

import matplotlib.pyplot as plt
from cycler import cycler

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

# Set the rcParams with color or linestyle
plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) +
   cycler('linestyle', [':', '-.', '-', '--'])))

# Plot the data points
plt.plot([0, 5, 2, 1])
plt.plot([2, 6, 3, 1])
plt.plot([3, 8, 5, 1])
plt.plot([4, 9, 0, 3])

plt.show()

輸出

它將生成以下輸出 −

更新日期: 19-10-2021

1K+ 瀏覽

開啟你的 職業生涯

學完課程,獲得認證

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