Matplotlib - 帶單位的橢圓



橢圓是一種幾何形狀,看起來像一個拉長的圓形。它由兩個關鍵屬性定義:長軸(最長直徑)和短軸(最短直徑)。這些軸在橢圓的中心相交,橢圓的形狀由這些軸的長度決定。長軸中心到邊緣的距離和短軸中心到邊緣的距離都很重要。

從單位的角度來看,你會使用某種單位來測量這些距離,例如英寸、釐米或你選擇的任何其他測量單位。因此,當我們說帶單位的橢圓時,我們的意思是我們使用具體的測量值(單位)來描述橢圓的大小,同時考慮長軸和短軸的長度。

Ellipse with Units

Matplotlib 中帶單位的橢圓

我們可以使用 "matplotlib.patches" 模組中的 "Ellipse" 類在 Matplotlib 中建立帶單位的橢圓。"Ellipse" 類允許你定義橢圓的中心、寬度和高度(長軸和短軸)、旋轉角度以及其他屬性,例如其旋轉角度。

軸的測量單位取決於你使用的座標系。例如,如果你使用英寸單位的繪圖,則長軸和短軸的長度將以英寸為單位。

資料座標系中固定大小的橢圓

在 Matplotlib 中,在資料座標系中建立固定大小的橢圓涉及在繪圖上繪製一個特定大小和位置由資料座標確定的橢圓。這意味著橢圓的尺寸以與你的實際資料相同的單位指定。

例如,如果你有一個包含 x 和 y 值的資料集,你可以繪製一個橢圓,其中心位於特定的 (x, y) 點,並且橢圓的寬度和高度以資料座標定義。

示例

我們正在繪製一個簡單的橢圓,其固定大小以資料座標指定。橢圓的中心位於繪圖上的 (3, 5) 位置,寬度設定為米為單位的“4”個單位,高度設定為“2”個單位。

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

# Creating a plot
fig, ax = plt.subplots()

# Ellipse in data coordinates (units: meters)
ellipse = Ellipse((3, 5), width=4, height=2, edgecolor='b', facecolor='none')
ax.add_patch(ellipse)

# Setting plot title and labels
ax.set_title('Fixed Size Ellipse in Data Coordinates')
ax.set_xlabel('X-axis (meters)')
ax.set_ylabel('Y-axis (meters)')

# Setting aspect ratio to 'equal'
ax.set_aspect('equal')

# Adjusting axis limits 
ax.set_xlim(0, 6)
ax.set_ylim(3, 7)

# Displaying dimensions 
plt.text(3, 5, f'Width: 4 meters\nHeight: 2 meters', ha='center', va='center', color='red', fontsize=10)
plt.show()

輸出

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

Fixed Size Ellipse in Data Coordinates

座標軸座標系中可變大小的橢圓

在 Matplotlib 中建立在座標軸座標系中具有可變大小的橢圓涉及繪製一個繪圖上的橢圓,其中尺寸以座標軸而不是實際資料來指定。換句話說,橢圓的寬度和高度分別作為 x 軸和 y 軸總長度的百分比給出。

例如,如果你想視覺化一個始終覆蓋 x 軸的 60% 和 y 軸的 30% 的橢圓,而不管具體資料值如何,你可以使用座標軸座標。當你想讓橢圓的大小相對於繪圖的整體尺寸時,這尤其有用。

示例

在這裡,我們正在繪製一個以座標軸座標指定可變大小的橢圓。橢圓的中心位於 (0.5, 0.5),對應於繪圖的中心。寬度和高度分別設定為相應軸長度的 60% 和 30%,允許橢圓隨著座標軸的變化而縮放。

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

# Creating a plot
fig, ax = plt.subplots()

# Ellipse in axes coordinates
ellipse = Ellipse((0.5, 0.5), width=0.6, height=0.3, edgecolor='r', facecolor='none', transform=ax.transAxes)
ax.add_patch(ellipse)

# Setting plot title and labels
ax.set_title('Ellipse with Variable Size in Axes Coordinates')
ax.set_xlabel('X-axis (normalized)')
ax.set_ylabel('Y-axis (normalized)')

# Displaying dimensions in the output
plt.text(0.5, 0.5, f'Width: 0.6 (normalized)\nHeight: 0.3 (normalized)', ha='center', va='center', color='blue', fontsize=10, transform=ax.transAxes)
plt.show()

輸出

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

Variable Size Axes Coordinates Ellipse

以圖形原點為中心的橢圓

在 Matplotlib 中建立以圖形原點為中心的橢圓涉及在繪圖上繪製一個橢圓,其中心位於整個圖形的原點。原點是圖形座標系中的 (0, 0) 點。在這種情況下,橢圓的寬度和高度以圖形的單位指定,橢圓的中心正好位於原點。

這演示瞭如何相對於整個圖形而不是單個座標軸來定位橢圓。

示例

在下面的示例中,我們正在建立一個以圖形原點 (0, 0) 為中心的橢圓。寬度設定為 4 個單位,高度設定為 2 個單位。

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import numpy as np

# Creating a plot
fig, ax = plt.subplots()

# Ellipse centered at figure origin
ellipse = Ellipse((0, 0), width=4, height=2, edgecolor='g', facecolor='none', transform=ax.transData)
ax.add_patch(ellipse)

# Marking the center with a marker
center_x, center_y = 0, 0
ax.plot(center_x, center_y, marker='o', markersize=8, color='red', label='Center')

# Dotted line to represent the center
ax.plot([center_x, center_x], [center_y, center_y], 'r--')

# Horizontal and vertical lines passing through the center
ax.axhline(center_y, color='blue', linestyle='--', label='Horizontal Line')
ax.axvline(center_x, color='purple', linestyle='--', label='Vertical Line')

# Setting plot title and labels
ax.set_title('Ellipse Centered at Figure Origin')
ax.set_xlabel('X-axis (units)')
ax.set_ylabel('Y-axis (units)')

# Adjusting axis limits to make the ellipse and lines visible
ax.set_xlim(-2, 2)
ax.set_ylim(-1, 1)

# Adding legend
ax.legend()
plt.show()

輸出

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

Ellipse Centered at Figure Origin

帶自定義角度的旋轉橢圓

在 Matplotlib 中建立帶自定義角度的旋轉橢圓涉及在繪圖上繪製一個橢圓並指定一個自定義角度來旋轉它。旋轉角度決定了橢圓在繪圖中的傾斜或旋轉方式。當你想突出顯示特定方向的特徵時,這很有用。

示例

現在,我們正在建立一個旋轉角度為“45”度的旋轉橢圓,從而得到一個傾斜的橢圓。橢圓的中心位於 (2, 3),寬度和高度以資料座標指定。

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

# Creating a plot
fig, ax = plt.subplots()

# Rotated ellipse with custom angle
ellipse = Ellipse((2, 3), width=3, height=1, angle=45, edgecolor='purple', facecolor='none', transform=ax.transData)
ax.add_patch(ellipse)

# Setting plot title and labels
ax.set_title('Rotated Ellipse with Custom Angle')
ax.set_xlabel('X-axis (units)')
ax.set_ylabel('Y-axis (units)')

# Setting aspect ratio to 'equal'
ax.set_aspect('equal')

# Adjusting axis limits 
ax.set_xlim(0, 4)
ax.set_ylim(2, 5)

# Adding grid for better visualization
ax.grid(True, linestyle='--', alpha=0.7)
plt.show()

輸出

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

Rotated Ellipse
廣告