Matplotlib - 基本單位



什麼是基本單位?

在 Matplotlib 中,基本單位指的是用於定義和測量繪圖各個方面(例如長度、寬度、位置和座標)的基本元素。瞭解這些單位對於準確地在繪圖中放置元素和控制其整體佈局至關重要。

Matplotlib 中常見的基本單位

以下是 Matplotlib 庫中常見的可用基本單位。

磅 (pt)

在 Matplotlib 中,**磅**通常指的是用於指定繪圖中各種視覺屬性(例如線寬、標記大小和文字大小)的度量單位。磅 (pt) 是排版和圖形學中的標準單位,經常因其與裝置無關的大小表示而被使用。

磅在 Matplotlib 中的用法

**線寬** - Matplotlib 函式(例如 **plt.plot()**)中的 **linewidth** 引數允許我們以磅為單位指定線的寬度。

**文字大小** - Matplotlib 函式 **plt.xlabel()、plt.ylabel()、plt.title()** 中的 **fontsize** 引數允許設定文字大小(以磅為單位)。

**標記大小** - plot 函式(例如 plt.plot())中的 **markersize** 引數以磅為單位定義標記的大小。

**圖形大小** - 使用 **plt.figure(figsize=(width, height))** 指定圖形大小時,尺寸以英寸為單位。但是,預設大小在不同系統上可能有所不同,例如 **1** 英寸並不總是等於 **72** 磅,但這是一種典型的約定。

磅與其他單位的轉換

Matplotlib 在內部使用磅,但在設定圖形大小或尺寸時,通常使用英寸。作為參考,**1** 英寸大約等於 **72** 磅。

  • 1 英寸 = 72 磅
  • 1 磅 ≈ 0.01389 英寸

理解和利用磅作為 Matplotlib 中的度量單位,可以精確控制繪圖中元素的視覺屬性,確保所需的顯示效果和可讀性。

示例

在這個例子中,我們為線圖設定了以磅為單位的線寬。

import matplotlib.pyplot as plt
plt.figure(figsize=(6, 4))  # 6 inches wide, 4 inches tall
plt.plot([1, 2, 3], [4, 5, 6], linewidth=3.5)  # Line width in points
plt.xlabel('X-axis', fontsize=12)  # Text size in points
plt.ylabel('Y-axis', fontsize=12)
plt.show()
輸出

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

Points

英寸 (in)

英寸用於在 Matplotlib 中指定圖形或子圖的整體大小。例如,**figsize = (width, height)**(以英寸為單位)定義圖形的寬度和高度。

示例

在這個例子中,我們以英寸為單位指定了圖形的大小。

import matplotlib.pyplot as plt
plt.figure(figsize=(4, 4))  # 6 inches wide, 4 inches tall
plt.plot([1, 2, 3], [4, 5, 6])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Example Plot with 4x4 Inches')
plt.show()
輸出

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

Inches

轉換

英寸與 Matplotlib 中常用的其他單位之間的轉換系數

1 英寸 ≈ 2.54 釐米

資料單位

資料單位表示沿 x 軸和 y 軸繪製的實際值。Matplotlib 使用這些單位將資料點繪製到圖表上。從資料單位到繪圖上物理單位的轉換由座標軸比例尺(例如線性、對數等)決定。

關於 Matplotlib 中資料單位的關鍵點

**數值資料** - 資料單位表示我們沿 x 軸和 y 軸繪製的數值。

**對映到繪圖空間** - Matplotlib 根據定義的座標軸限制和縮放比例,將這些數值資料值對映到繪圖空間中的特定位置。

**繪製資料** - 當我們使用 Matplotlib 繪製資料時,我們必須使用數值資料指定 x 和 y 座標。Matplotlib 會處理這些資料點在繪圖中的縮放和定位。

**座標軸限制** - 使用 **xlim** 設定 x 軸限制,使用 **ylim** 設定 y 軸限制,定義繪圖中顯示的資料單位範圍。

**座標軸刻度** - 座標軸上的刻度表示特定資料單位,為繪圖中存在的資料範圍提供視覺參考。

示例

在這個例子中,我們設定了繪圖的資料單位。

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Plotting using data units
plt.plot(x, y, marker='o')

# Setting axis limits in data units
plt.xlim(0, 6)  # X-axis limits from 0 to 6
plt.ylim(0, 12)  # Y-axis limits from 0 to 12
plt.xlabel('X-axis (Data Units)')
plt.ylabel('Y-axis (Data Units)')
plt.title('Plot with Data Units')
plt.show()
輸出

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

Data Units

理解 Matplotlib 中的資料單位對於準確地視覺化資料並確保繪製的表示與底層數值正確對應至關重要。

座標軸座標

座標軸座標的範圍是從 0.0 到 1.0,它指定單個子圖或座標軸內的相對位置。**(0, 0)** 表示子圖的左下角,**(1, 1)** 表示子圖的右上角。

座標軸座標提供了一種靈活的方法,可以在子圖內相對於其大小和邊界來定位註釋、文字或其他元素,確保在不同繪圖大小或尺寸下的一致性和響應性定位。

示例

在這個例子中,我們設定了繪圖的座標軸座標 x 和 y。

import matplotlib.pyplot as plt

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

# Plotting data
ax.plot([1, 2, 3], [2, 4, 3])

# Annotating at axes coordinates
ax.annotate('Important Point', 
   xy=(2, 4),               # Data point to annotate
   xytext=(0.5, 0.5),       # Position in axes coordinates
   textcoords='axes fraction',  # Specifying axes coordinates
   arrowprops=dict(facecolor='black', arrowstyle='->'))
plt.show()
輸出

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

Axes

圖形座標

圖形座標的範圍也是從 **0.0** 到 **1.0**,但是它相對於整個圖形畫布指定位置。**(0, 0)** 是整個圖形的左下角,**(1, 1)** 是整個圖形的右上角。

示例

這是一個設定繪圖圖形座標的示例。

import matplotlib.pyplot as plt

# Creating subplots
fig, (ax1, ax2) = plt.subplots(1, 2)

# Plotting data in subplot 1
ax1.plot([1, 2, 3], [2, 4, 3])

# Plotting data in subplot 2
ax2.plot([1, 2, 3], [3, 1, 4])

# Annotating at figure coordinates
fig.text(0.5, 0.9, 'Figure Annotation', ha='center', fontsize=12, color='red')
plt.show()
輸出

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

Figure Coordinates

使用 Matplotlib 繪製所有基本單位的圖形

此示例展示了在 Matplotlib 中使用各種單位,例如英寸表示圖形大小,磅表示文字大小,資料單位表示繪製資料點,以及座標軸座標表示在子圖中定位註釋。

示例

import matplotlib.pyplot as plt

# Define a figure with specified size in inches
fig = plt.figure(figsize=(6, 4))  # Width: 6 inches, Height: 4 inches

# Create an axis within the figure
ax = fig.add_subplot(111)

# Plotting data points using data units
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
ax.plot(x, y, marker='o')

# Setting labels and titles using text in points
ax.set_xlabel('X-axis Label', fontsize=12)
ax.set_ylabel('Y-axis Label', fontsize=12)
ax.set_title('Plot Title', fontsize=14)

# Setting the position of a text annotation using axes coordinates
ax.text(0.5, 0.5, 'Annotation', transform=ax.transAxes, fontsize=10)
plt.show()
輸出

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

Overall Units
廣告