
- Matplotlib 基礎
- Matplotlib - 首頁
- Matplotlib - 簡介
- Matplotlib - 與Seaborn對比
- Matplotlib - 環境設定
- Matplotlib - Anaconda發行版
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - 簡單繪圖
- Matplotlib - 儲存圖形
- Matplotlib - 標記
- Matplotlib - 圖形
- Matplotlib - 樣式
- Matplotlib - 圖例
- Matplotlib - 顏色
- Matplotlib - 顏色圖
- Matplotlib - 顏色圖歸一化
- Matplotlib - 選擇顏色圖
- Matplotlib - 顏色條
- Matplotlib - 文字
- Matplotlib - 文字屬性
- Matplotlib - 子圖示題
- Matplotlib - 影像
- Matplotlib - 影像蒙版
- Matplotlib - 註釋
- Matplotlib - 箭頭
- Matplotlib - 字型
- Matplotlib - 什麼是字型?
- 全域性設定字型屬性
- Matplotlib - 字型索引
- Matplotlib - 字型屬性
- Matplotlib - 刻度
- Matplotlib - 線性和對數刻度
- Matplotlib - 對稱對數和Logit刻度
- Matplotlib - LaTeX
- Matplotlib - 什麼是LaTeX?
- Matplotlib - LaTeX用於數學表示式
- Matplotlib - LaTeX在註釋中的文字格式化
- Matplotlib - PostScript
- 在註釋中啟用LaTeX渲染
- Matplotlib - 數學表示式
- Matplotlib - 動畫
- Matplotlib - 圖形物件
- Matplotlib - 使用Cycler進行樣式設定
- Matplotlib - 路徑
- Matplotlib - 路徑效果
- Matplotlib - 轉換
- Matplotlib - 刻度和刻度標籤
- Matplotlib - 弧度刻度
- Matplotlib - 日期刻度
- Matplotlib - 刻度格式化器
- Matplotlib - 刻度定位器
- Matplotlib - 基本單位
- Matplotlib - 自動縮放
- Matplotlib - 反轉座標軸
- Matplotlib - 對數座標軸
- Matplotlib - Symlog
- Matplotlib - 單位處理
- Matplotlib - 帶單位的橢圓
- Matplotlib - 脊柱
- Matplotlib - 座標軸範圍
- Matplotlib - 座標軸刻度
- Matplotlib - 座標軸刻度
- Matplotlib - 格式化座標軸
- Matplotlib - Axes類
- Matplotlib - 雙座標軸
- Matplotlib - Figure類
- Matplotlib - 多圖
- Matplotlib - 網格
- Matplotlib - 面向物件介面
- Matplotlib - PyLab模組
- Matplotlib - Subplots()函式
- Matplotlib - Subplot2grid()函式
- Matplotlib - 錨定圖形物件
- Matplotlib - 手動等值線
- Matplotlib - 座標報告
- Matplotlib - AGG過濾器
- Matplotlib - 帶狀框
- Matplotlib - 填充螺旋線
- Matplotlib - Findobj演示
- Matplotlib - 超連結
- Matplotlib - 影像縮圖
- Matplotlib - 使用關鍵字繪圖
- Matplotlib - 建立Logo
- Matplotlib - 多頁PDF
- Matplotlib - 多程序
- Matplotlib - 列印標準輸出
- Matplotlib - 複合路徑
- Matplotlib - Sankey類
- Matplotlib - MRI與EEG
- Matplotlib - 樣式表
- Matplotlib - 背景顏色
- Matplotlib - Basemap
- Matplotlib事件處理
- Matplotlib - 事件處理
- Matplotlib - 關閉事件
- Matplotlib - 滑鼠移動
- Matplotlib - 點選事件
- Matplotlib - 滾動事件
- Matplotlib - 按鍵事件
- Matplotlib - 選擇事件
- Matplotlib - 透鏡
- Matplotlib - 路徑編輯器
- Matplotlib - 多邊形編輯器
- Matplotlib - 定時器
- Matplotlib - Viewlims
- Matplotlib - 縮放視窗
- Matplotlib小部件
- Matplotlib - 遊標小部件
- Matplotlib - 帶註釋的遊標
- Matplotlib - 按鈕小部件
- Matplotlib - 複選框
- Matplotlib - 套索選擇器
- Matplotlib - 選單小部件
- Matplotlib - 滑鼠遊標
- Matplotlib - 多遊標
- Matplotlib - 多邊形選擇器
- Matplotlib - 單選按鈕
- Matplotlib - 範圍滑塊
- Matplotlib - 矩形選擇器
- Matplotlib - 橢圓選擇器
- Matplotlib - 滑塊小部件
- Matplotlib - 跨度選擇器
- Matplotlib - 文字框
- Matplotlib繪圖
- Matplotlib - 條形圖
- Matplotlib - 直方圖
- Matplotlib - 餅圖
- Matplotlib - 散點圖
- Matplotlib - 箱線圖
- Matplotlib - 小提琴圖
- Matplotlib - 等值線圖
- Matplotlib - 3D繪圖
- Matplotlib - 3D等值線
- Matplotlib - 3D線框圖
- Matplotlib - 3D表面圖
- Matplotlib - Quiver圖
- Matplotlib有用資源
- Matplotlib - 快速指南
- Matplotlib - 有用資源
- Matplotlib - 討論
Matplotlib - 使用Cycler進行樣式設定
Cycler 是從Matplotlib中提取的一個獨立包,它旨在控制繪圖的樣式屬性,如顏色、標記和線型。此工具允許您輕鬆地迴圈遍歷不同的樣式,以便在一個軸上繪製多條線。
匯入Cycler - 要開始使用Cycler,您需要將其匯入到Python指令碼中。
from cycler import cycler
這使您能夠建立和操作Cycler以設定繪圖樣式。
建立Cycler - cycler函式用於建立一個新的Cycler物件。它可以帶一個位置引數、一對位置引數或關鍵字引數的組合來呼叫。
# Creating a color Cycler color_cycle = cycler(color=['r', 'g', 'b'])
"color_cycle" 是一個Cycler物件,它迴圈遍歷紅色、綠色和藍色。獲得Cycler後,您可以將其連結到matplotlib的繪圖屬性。
迴圈遍歷多個屬性
Cycler包提供了用於組合和操作多個Cycler以建立複雜樣式變化的高階操作。這意味著您可以將cycler加在一起或將它們相乘以組合不同的屬性。
以下是cycler中的不同操作:
- Cycler加法 - 使用+運算子可以組合多個Cycler物件。例如:
cycler(color=['r', 'g', 'b']) + cycler(linestyle=['-', '--', ':'])
- Cycler乘法 - 可以將Cycler相乘以建立更廣泛的獨特樣式。例如
cycler(color=['r', 'g', 'b']) * cycler(linestyle=['-', '--', ':'])
- 整數乘法 - 可以將Cycler物件乘以整數值以增加其長度。cycler * 2 和 2 * cycler 都產生相同的結果,重複元素。以下是語法
color_cycle * 2
- Cycler連線 - 可以使用Cycler.concat()方法或頂級concat()函式連線Cycler物件。
在本教程中,我們將探討兩種不同的方法,使用Cycler包自定義Matplotlib中繪圖的樣式。
- 設定預設屬性迴圈(rc引數) - 這是全域性設定,它確保每個後續繪圖都將設定為指定的樣式。
- 設定單個軸對的屬性迴圈 - 這是區域性設定,它將自定義屬性迴圈專門應用於特定的一組軸。
設定預設屬性迴圈(rc引數)
在matplotlib中,可以使用matplotlib.pyplot.rc()方法為所有將來的繪圖指定預設樣式,這將設定繪圖和軸中線條的預設迴圈器。這意味著您將來建立的每個繪圖都將遵循此顏色和線型迴圈,除非您覆蓋它。
示例1
這是一個基本的示例,演示瞭如何為多個繪圖迴圈遍歷不同的線型。此處使用plt.rc()方法設定繪圖的預設線型。
import matplotlib.pyplot as plt from cycler import cycler # Set the property cycle for the linestyle of lines in the axes linestyle_cycler = cycler('linestyle', ['-', ':', '-.']) plt.rc('axes', prop_cycle=linestyle_cycler) # Create multiple plots using a loop for i in range(5): x = range(i, i + 5) plt.plot(range(5), x) # Display the plot plt.legend(['first', 'second', 'third', 'fourth', 'fifth'], loc='upper left', fancybox=True, shadow=True) plt.show()
輸出
執行上述程式碼後,我們將獲得以下輸出:

讓我們透過將多個(顏色和線型)迴圈器加在一起(+符號)來組合它們。
示例2
此示例演示瞭如何使用Cycler為您的繪圖定義預設樣式(迴圈遍歷顏色和線型),從而可以輕鬆地視覺化所有具有不同顏色('r','g','b','y')和線型('-','--',':','-.')的繪圖。
from cycler import cycler import matplotlib.pyplot as plt import numpy as np # Generate sample data x = np.linspace(0, 2 * np.pi, 50) offsets = np.linspace(0, 2 * np.pi, 4, endpoint=False) yy = np.transpose([np.sin(x + phi) for phi in offsets]) # Set default prop_cycle default_cycler = (cycler(color=['r', 'g', 'b', 'y']) + cycler(linestyle=['-', '--', ':', '-.'])) plt.rc('lines', linewidth=4) plt.rc('axes', prop_cycle=default_cycler) # Plot with the default color cycle plt.plot(yy) plt.title('Set Default Color Cycle') plt.show()
輸出
執行上述程式碼後,我們將獲得以下輸出:

設定單個軸對的屬性迴圈
自定義圖形中特定軸對的樣式,而不影響其他軸。您可以使用matplotlib.axes.Axes.set_prop_cycle()應用此自定義迴圈器。這意味著只有這組特定的軸上的繪圖才會遵循指定的顏色和線寬迴圈。
示例
在此示例中,ax0上的第一組繪圖遵循預設的顏色和線型迴圈。ax1上的第二組繪圖使用為該軸專門定義的自定義顏色和線寬迴圈,使用set_prop_cycle()方法。
from cycler import cycler import matplotlib.pyplot as plt import numpy as np # Generate sample data x = np.linspace(0, 2 * np.pi, 50) offsets = np.linspace(0, 2 * np.pi, 4, endpoint=False) yy = np.transpose([np.sin(x + phi) for phi in offsets]) # Define a default cycler for colors and linestyles default_cycler = (cycler(color=['r', 'g', 'b', 'y']) + cycler(linestyle=['-', '--', ':', '-.'])) # Set the default linewidth for lines in all plots plt.rc('lines', linewidth=4) # Set the default property cycle for axes to the default cycler plt.rc('axes', prop_cycle=default_cycler) # Create a figure and two axes fig, (ax0, ax1) = plt.subplots(nrows=2, figsize=(7, 8)) # Plot on the first axis using the default color cycle ax0.plot(yy) ax0.set_title('Default Color Cycle: rgby') # Define a custom cycler custom_cycler = (cycler(color=['c', 'm', 'y', 'k']) + cycler(lw=[1, 2, 3, 4])) # Set the custom property cycle for the second axis ax1.set_prop_cycle(custom_cycler) # Plot on the second axis using the custom color and linewidth cycle ax1.plot(yy) ax1.set_title('Custom Color Cycle: cmyk') # Add space between the two plots fig.subplots_adjust(hspace=0.3) # Show the plots plt.show()
輸出
執行上述程式碼後,我們將獲得以下輸出:
