
- 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 - 簡介
Matplotlib 是 Python 中一個強大且廣泛使用的繪相簿,它使我們能夠建立各種靜態、互動式和出版質量的繪圖和視覺化效果。它被廣泛用於資料視覺化任務,並提供各種功能來建立諸如折線圖、散點圖、條形圖、直方圖、3D 圖等繪圖。Matplotlib 庫提供了靈活性和自定義選項,可以根據特定需求定製我們的繪圖。
它是一個跨平臺庫,用於根據陣列中的資料繪製二維圖。Matplotlib 使用 Python 編寫,並利用 NumPy,它是 Python 的數值數學擴充套件。它提供了一個面向物件的 API,有助於使用 Python GUI 工具包(如 PyQt、WxPython 或 Tkinter)將繪圖嵌入應用程式中。它可以在 Python 和 IPython shell 中使用。Jupyter Notebook 和 Web 應用程式伺服器也可以。
Matplotlib 具有一個名為 Pylab 的過程介面,該介面旨在類似於 MATLAB,MATLAB 是 MathWorks 開發的一種專有程式語言。Matplotlib 以及 NumPy 可以被認為是 MATLAB 的開源等價物。
Matplotlib 最初由 John D. Hunter 於 2003 年編寫。當前穩定版本是 2018 年 1 月釋出的 2.2.0。
使用 Matplotlib 最常見的方法是透過其 pyplot 模組。
以下是 Matplotlib 關鍵元件和功能的深入概述:
Matplotlib 的元件

圖形
圖形是顯示我們的繪圖或繪圖集合的整個視窗或頁面。它充當一個容器,包含圖形表示的所有元素,包括軸、標籤、圖例和其他元件。
示例
這是表示圖形的基本繪圖。
import matplotlib.pyplot as plt # Create a new figure fig = plt.figure() # Add a plot or subplot to the figure plt.plot([1, 2, 3], [4, 5, 6]) plt.show()
輸出

軸/子圖
圖形中繪製資料的特定區域。圖形可以包含多個軸或子圖。以下是軸/子圖的示例。
示例
import matplotlib.pyplot as plt # Creating a 2x2 grid of subplots fig, axes = plt.subplots(nrows=2, ncols=2) # Accessing individual axes (subplots) axes[0, 0].plot([1, 2, 3], [4, 5, 6]) # Plot in the first subplot (top-left) axes[0, 1].scatter([1, 2, 3], [4, 5, 6]) # Second subplot (top-right) axes[1, 0].bar([1, 2, 3], [4, 5, 6]) # Third subplot (bottom-left) axes[1, 1].hist([1, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5]) # Fourth subplot (bottom-right) plt.show()
輸出

座標軸
座標軸指的是繪圖中的 X 軸或 Y 軸,或者它也可以表示一組子圖中的單個軸。瞭解軸對於控制和自定義 Matplotlib 中繪圖的外觀和行為至關重要。以下是包含座標軸的繪圖。
示例
import matplotlib.pyplot as plt # Creating a plot plt.plot([1, 2, 3, 4], [10, 20, 25, 30]) # Customizing axis limits and labels plt.xlim(0, 5) plt.ylim(0, 35) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.show()
輸出

圖形物件
圖形物件指的是構成繪圖的各種元件或實體,例如圖形、軸、線條、文字、補丁、形狀(矩形或圓形)等。它們是用於建立視覺化的構建塊,並以層次結構組織。
以下是類似於圖形物件所有元件的繪圖。
示例
import matplotlib.pyplot as plt # Create a figure and an axis (subplot) fig, ax = plt.subplots() # Plot a line (artist) line = ax.plot([1, 2, 3], [4, 5, 6], label='Line')[0] # Modify line properties line.set_color('red') line.set_linewidth(2.5) # Add labels and title (text artists) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_title('Artist Plot') plt.legend() plt.show()
輸出

主要特徵
簡單繪圖 - Matplotlib 允許我們僅用幾行程式碼輕鬆建立基本繪圖。
自定義 - 我們可以透過調整顏色、線型、標記、標籤、標題等來廣泛自定義繪圖。
多種繪圖型別 - 它支援各種繪圖型別,例如折線圖、散點圖、條形圖、直方圖、餅圖、3D 圖等。
出版質量 - Matplotlib 生成適合出版物和簡報的高質量繪圖,並具有可自定義的 DPI 設定。
支援 LaTeX 排版 - 我們可以使用 LaTeX 格式化繪圖中的文字和數學表示式。
繪圖型別
Matplotlib 支援各種型別的繪圖,如下所述。每種繪圖型別在庫中都有自己的函式。
繪圖名稱 | 定義 | 影像 |
---|---|---|
折線圖 | 折線圖是一種圖形,它顯示由直線段連線的資料點。 matplotlib 庫的 plt.plot() 函式用於建立折線圖。 |
![]() |
散點圖 | 散點圖是一種圖形,它透過在二維平面上顯示為標記來表示單個數據點。 plt.scatter() 函式用於繪製散點圖。 |
![]() |
折線圖 | 折線圖是一種圖形,它顯示由直線段連線的資料點。 matplotlib 庫的 plt.plot() 函式用於建立折線圖。 |
![]() |
條形圖 | 條形圖或條形圖是使用矩形條形對分類資料進行視覺化表示。 plt.bar() 函式用於繪製條形圖。 |
![]() |
餅圖 | 餅圖也稱為餅圖。它是一種圓形統計圖形,用於說明數值比例。它將一個圓分成扇形或切片,以表示資料集中類別的大小或百分比。 plt.pie() 函式用於繪製餅圖。 |
![]() |
上面提到的都是 matplotlib 庫的基本繪圖。我們還可以藉助 Matplotlib 視覺化 3D 繪圖。
子圖
我們可以使用子圖在一個圖形中建立多個繪圖。當我們想要一起顯示多個繪圖時,這很有用。
儲存繪圖
Matplotlib 允許我們以各種格式儲存我們的繪圖,例如 PNG、PDF、SVG 等。