Matplotlib - 路徑



路徑是指引導從一個地方到另一個地方移動的路線(軌跡)。它可以是物理的,例如小徑或道路,也可以是抽象的,例如實現目標的一系列步驟。路徑提供方向並幫助我們瀏覽空間。它們標誌著到達目的地的途徑。

Paths

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()

輸出

以下是上述程式碼的輸出 -

Straight Line Path

圓形路徑

在 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()

輸出

執行上述程式碼後,我們將得到以下輸出 -

Circle Path

貝塞爾曲線路徑

在 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()

輸出

執行上述程式碼後,我們將得到以下輸出 -

Bezier Curve Path

多邊形路徑

在 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()

輸出

執行上述程式碼後,我們將得到以下輸出 -

Polygon Path
廣告