
- 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 - 座標軸類
- Matplotlib - 雙座標軸
- Matplotlib - 圖形類
- 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 - RangeSlider
- Matplotlib - 矩形選擇器
- Matplotlib - 橢圓選擇器
- Matplotlib - 滑塊小部件
- Matplotlib - 區間選擇器
- Matplotlib - 文字框
- Matplotlib 繪圖
- Matplotlib - 條形圖
- Matplotlib - 直方圖
- Matplotlib - 餅圖
- Matplotlib - 散點圖
- Matplotlib - 箱線圖
- Matplotlib - 小提琴圖
- Matplotlib - 等高線圖
- Matplotlib - 3D 繪圖
- Matplotlib - 3D 等高線
- Matplotlib - 3D 線框圖
- Matplotlib - 3D 表面圖
- Matplotlib - 矢羽圖
- Matplotlib 有用資源
- Matplotlib - 快速指南
- Matplotlib - 有用資源
- Matplotlib - 討論
Matplotlib - 圖形類
在 Matplotlib 中,Figure 是一個頂級容器,它包含繪圖或視覺化中的所有元素。它是包含各種元件(如座標軸、標籤、標題、圖例、顏色條和其他元素)的整體視窗或畫布。
參見下圖以供參考:

在上圖中,綠色區域表示圖形,白色區域是座標軸區域。
Matplotlb 中的圖形類
Matplotlib 中的Figure() 類是一個頂級圖形物件,充當所有繪圖元素的主要容器。它將所有內容組合在一起,包括子圖、座標軸、標題、圖例和其他圖形元素。
此類可在matplotlib.figure 模組中找到,除了 Figure() 類之外,該模組還包含與建立和管理圖形相關的類,並提供多種自定義選項。
建立圖形
通常使用 pyplot 方法(如 figure、subplots 和 subplot_mosaic)建立 Figure 例項。這些方法同時返回 Figure 例項和一組 Axes,提供了一種方便的方式來建立和使用視覺化效果。
示例
這是一個使用pyplot.figure() 方法建立圖形的示例。
import matplotlib.pyplot as plt import numpy as np # Creating the Figure instance fig = plt.figure(figsize=[7, 3], facecolor='lightgreen', layout='constrained') # Adding a title to the Figure fig.suptitle('Figure') # Adding a subplot (Axes) to the Figure ax = fig.add_subplot() # Setting a title for the subplot ax.set_title('Axes', loc='left', fontstyle='oblique', fontsize='medium') # Showing the plot plt.show()
輸出
執行上述程式碼後,我們將得到以下輸出:

示例
此示例演示瞭如何在 Matplotlib 的單個指令碼中分別建立多個圖形。
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7, 3.50] plt.rcParams["figure.autolayout"] = True # Create Figure 1 fig1 = plt.figure("Figure 1") plt.plot([1, 3, 7, 3, 1], c="red", lw=2) plt.title("Figure 1") # Create Figure 2 fig2 = plt.figure("Figure 2") plt.plot([1, 3, 7, 3, 1], c="green", lw=5) plt.title("Figure 2") # Display both figures plt.show()
輸出
執行上述程式碼後,您將得到以下輸出:


建立帶有子圖網格的圖形
建立圖形時,可以自定義各種選項,包括子圖、大小、解析度、顏色和佈局。Figure 類的屬性(如 figsize、dpi、facecolor、edgecolor、linewidth 和 layout)在塑造視覺化的外觀方面起著至關重要的作用。
示例
這是一個使用pyplot.subplots() 方法建立 2x2 子圖網格以及多個自定義選項的示例。
import matplotlib.pyplot as plt import numpy as np # Create a 2x2 grid of subplots with various customization options fig, axs = plt.subplots(2, 2, figsize=(7, 4), facecolor='lightgreen', layout='constrained') # Super title for the entire figure fig.suptitle('2x2 Grid of Subplots', fontsize='x-large') # Display the Figure plt.show()
輸出
執行上述程式碼後,我們將得到以下輸出:

示例
這是另一個使用plt.subplot_mosaic() 方法建立更復雜佈局的示例。
import matplotlib.pyplot as plt # Create a more complex layout using plt.subplot_mosaic() fig, axs = plt.subplot_mosaic([['A', 'right'], ['B', 'right']], facecolor='lightgreen', layout='constrained') # Add text to each subplot for ax_name, ax in axs.items(): ax.text(0.5, 0.5, ax_name, ha='center', va='center', fontsize='large', fontweight='bold', color='blue') # Super title for the entire figure fig.suptitle('Complex Layout using subplot_mosaic()', fontsize='x-large') # Display the Figure plt.show()
輸出
執行上述程式碼後,我們將得到以下輸出:

儲存圖形
完成視覺化後,可以使用savefig() 方法輕鬆地將圖形儲存到磁碟。此方法允許您指定檔案格式(例如,PNG、PDF)並自定義解析度和邊界框等選項。
示例
讓我們來看一個儲存 Figure 物件的簡單示例。
import matplotlib.pyplot as plt # Create a 2x2 grid of subplots with various customization options fig, axs = plt.subplots(2, 2, figsize=(7, 4), facecolor='lightgreen', layout='constrained') # Super title for the entire figure fig.suptitle('2x2 Grid of Subplots', fontsize='x-large') # Super title for the entire figure fig.suptitle('Saving a Figure', fontsize='x-large') # Display the Figure plt.show() # Save the Figure object to a file fig.savefig('Saved Figure.png', dpi=300)
輸出
執行上述程式後,以下圖形將儲存在您的工作目錄中:
