
- 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 - RangeSlider
- 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中的路徑
在 Matplotlib 中,路徑是用於在繪圖上繪製形狀和線條的基本物件。路徑由一系列連線的點組成,稱為頂點或節點,以及關於如何連線這些點以形成形狀(如線、曲線或多邊形)的指令。
可以使用“Path”類在 Matplotlib 中建立路徑。這些路徑可以用於建立各種型別的繪圖,例如線、曲線、多邊形,甚至自定義形狀。路徑提供了一種定義繪圖中物件外觀的方法,允許您控制它們的大小、位置和樣式。
讓我們從繪製一條基本的直線路徑開始。
直線路徑
在 Matplotlib 中,直線路徑指的是由兩點用直線連線的基本幾何元素。此路徑是透過指定線的起點(通常稱為“原點”)和終點的座標來建立的。然後,Matplotlib 在這兩點之間繪製一條直線,形成路徑。
示例
在下面的示例中,我們使用 Matplotlib 中的 Path 類建立一個直線路徑。我們定義了兩個頂點,分別表示線的起點和終點。此外,我們指定路徑程式碼以指示路徑移動到第一個頂點,然後建立一條線到第二個頂點。透過使用這些頂點和程式碼構造路徑,我們實現了直線的表示 -
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches # Defining the vertices for the straight line path verts = [ # Starting point (0, 0), # Ending point (1, 1) ] # Defining the codes for the straight line path codes = [Path.MOVETO, Path.LINETO] # Creating the straight line path path = Path(verts, codes) # Plotting the path fig, ax = plt.subplots() patch = patches.PathPatch(path, facecolor='none', lw=2) ax.add_patch(patch) ax.set_title('Straight Line Path') ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.grid(True) plt.show()
輸出
以下是上述程式碼的輸出 -

圓形路徑
在 Matplotlib 中,圓形路徑表示繪圖上繪製的圓形。它是透過指定圓的中心點及其半徑來建立的。然後,Matplotlib 在圓的圓周周圍生成一系列點,並將它們連線起來形成一個封閉的路徑。
示例
在這裡,我們使用 Matplotlib 中的 Path 類建立一個圓形路徑。我們透過定義在單位圓周圍均勻分佈的角度來生成圓周上的一系列頂點。使用三角函式,我們計算這些頂點的座標。隨後,我們定義路徑程式碼以用線連線這些頂點 -
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches import numpy as np # Defining the angles for the circle path theta = np.linspace(0, 2*np.pi, 100) # Defining the vertices for the circle path verts = np.column_stack([np.cos(theta), np.sin(theta)]) # Definng the codes for the circle path codes = [Path.MOVETO] + [Path.LINETO] * (len(verts) - 1) # Creating the circle path path = Path(verts, codes) # Plotting the path fig, ax = plt.subplots() patch = patches.PathPatch(path, facecolor='none', lw=2) ax.add_patch(patch) ax.set_title('Circle Path') ax.set_xlim(-1.1, 1.1) ax.set_ylim(-1.1, 1.1) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.grid(True) plt.show()
輸出
執行上述程式碼後,我們將得到以下輸出 -

貝塞爾曲線路徑
在 Matplotlib 中,貝塞爾曲線路徑是用控制點建立的平滑曲線。
要構造貝塞爾曲線,您需要指定一系列控制點來引導曲線的路徑。這些控制點決定曲線的走向和形狀,使您可以建立平滑流暢的形狀。然後,Matplotlib 透過連線這些控制點來生成曲線。
示例
現在,我們使用 Path 類在 Matplotlib 中建立一個貝塞爾曲線路徑。我們指定定義貝塞爾曲線形狀的控制點。此外,我們定義路徑程式碼以指示連續控制點之間曲線段的型別。透過使用這些控制點和程式碼構造路徑,我們建立了一個平滑的貝塞爾曲線 -
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches # Defining the control points for the Bezier curve verts = [ (0.1, 0.1), (0.2, 0.8), (0.8, 0.8), (0.9, 0.1), ] # Defining the codes for the Bezier curve path codes = [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4] # Creating the Bezier curve path path = Path(verts, codes) # Plotting the path fig, ax = plt.subplots() patch = patches.PathPatch(path, facecolor='none', lw=2) ax.add_patch(patch) ax.set_title('Bezier Curve Path') ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.grid(True) plt.show()
輸出
執行上述程式碼後,我們將得到以下輸出 -

多邊形路徑
在 Matplotlib 中,多邊形路徑表示具有多條直邊的封閉形狀,類似於幾何多邊形。此形狀是透過按特定順序指定其頂點或角點的座標來建立的。然後,Matplotlib 順序連線這些點以形成多邊形的邊,並透過連線最後一個點到第一個點來閉合路徑。
多邊形路徑是用於表示各種形狀的多功能元素,例如三角形、矩形、五邊形或任何其他多邊形。它們可以用顏色填充以表示區域,或用作輪廓以突出顯示繪圖中的特定區域。
示例
在此示例中,我們使用 Matplotlib 中的 Path 類建立一個多邊形路徑。我們定義了多邊形的頂點,表示多邊形形狀的角點。此外,我們指定路徑程式碼以連線這些頂點並閉合多邊形。透過使用頂點和程式碼構造路徑,我們形成了一個封閉的多邊形形狀 -
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches # Defining the vertices for the polygon verts = [ (0.1, 0.1), (0.5, 0.9), (0.9, 0.1), # Closing the polygon (0.1, 0.1) ] # Defining the codes for the polygon path codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY] # Creating the polygon path path = Path(verts, codes) # Plotting the path fig, ax = plt.subplots() patch = patches.PathPatch(path, facecolor='none', lw=2) ax.add_patch(patch) ax.set_title('Polygon Path') ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.grid(True) plt.show()
輸出
執行上述程式碼後,我們將得到以下輸出 -
