Matplotlib - 座標變換



一般來說,變換是將某事物從一種形式轉換為另一種形式的方法。這就像接收一個輸入並將其轉換為不同的輸出。變換被廣泛應用於數學、物理和計算機科學等各個領域。

例如,在數學中,變換可以是一種將一組數字或函式轉換為不同表示的數學運算。例如,傅立葉變換將時域函式(如聲波)轉換為其頻率分量。

Transforms

Matplotlib中的變換

在 Matplotlib 中,變換是指將資料座標轉換為畫素座標的過程,它允許精確地將圖形元素(如點、線和文字)放置在繪圖中。

您可以在 matplotlib 中使用各種型別的變換,例如線性變換、對數變換等等。為了處理這些變換,Matplotlib 提供了“transform”引數。

“transform”引數可以取各種值,例如“matplotlib.transforms.Transform”類的例項或表示常見變換的字串,例如資料座標的'ax.transData'或座標軸座標的'ax.transAxes'。透過使用transform引數,您可以自定義座標系並應用不同的變換來增強 Matplotlib 繪圖上資料視覺化的效果。

資料到座標軸變換

在 Matplotlib 中,資料到座標軸變換是指將您的實際資料點轉換為繪圖座標軸內座標的過程。當您繪製資料時,您使用特定的“x”和“y”值。變換確保這些資料點在繪圖的座標軸內被正確定位,同時考慮到“x”和“y”座標軸的縮放和限制。

示例

在下面的示例中,我們使用資料座標 (x, y) 建立一個簡單的線圖。然後,我們使用 ax.text() 函式向繪圖新增文字,在資料座標中指定位置 (2, 25)。“transform=ax.transData”引數確保文字使用資料座標定位 -

import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

# Creating a plot
fig, ax = plt.subplots()
ax.plot(x, y, marker='o', linestyle='-', label='Data points')
ax.legend()

# Transforming data coordinates to axes coordinates
ax.text(2, 25,'Text in Data Coordinates' , transform=ax.transData)

# Setting plot title and labels
ax.set_title('Data to Axes Transform')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

plt.show()
輸出

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

Data to Axes Transform

座標軸到資料變換

Matplotlib 中的座標軸到資料變換是“資料到座標軸變換”的反向過程。“資料到座標軸變換”將您的實際資料點轉換為繪圖座標軸內的座標,而“座標軸到資料變換”則執行相反的操作。它採用根據繪圖座標軸指定的座標,並將其轉換為相應的資料值。

示例

在這裡,我們建立一個繪圖,並將“x”和“y”座標軸的座標軸限制設定為“0”到“1”。我們使用 ax.text() 函式向繪圖新增文字,在座標軸座標中指定位置“(0.5, 0.5)”。“transform=ax.transAxes”引數確保文字位置被解釋為相對於座標軸而不是資料 -

import matplotlib.pyplot as plt

# Creating a plot with normalized axes
fig, ax = plt.subplots()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)

# Transforming axes coordinates to data coordinates
ax.text(0.5, 0.5, 'Text in Axes Coordinates', transform=ax.transAxes, ha='center', va='center')

# Setting plot title and labels
ax.set_title('Axes to Data Transform Example')
ax.set_xlabel('X-axis (normalized)')
ax.set_ylabel('Y-axis (normalized)')

plt.show()
輸出

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

Axes to Data Transform

混合變換

在 Matplotlib 中,混合變換是在繪圖上放置元素時組合或混合不同座標系的一種方法。它允許您建立一個包含資料座標和座標軸座標方面的自定義變換。

示例

在下面的示例中,我們建立一個繪圖並使用 blended_transform_factory() 函式建立一個混合變換。“trans”物件是資料座標和座標軸座標的混合。然後,ax.text() 函式使用此混合變換在 (0.8, 0.2) 處定位文字,有效地組合了資料和座標軸座標 -

import matplotlib.pyplot as plt
from matplotlib.transforms import blended_transform_factory

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

# Creating a blended transformation
trans = blended_transform_factory(ax.transData, ax.transAxes)

# Adding text using the blended transformation
ax.text(0.8, 0.2, 'Blended Transform', transform=trans, ha='center', va='center')

# Setting plot title and labels
ax.set_title('Blended Transform')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

plt.show()
輸出

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

Blended Transform

偏移變換

在 Matplotlib 中,偏移變換是在繪圖上新增精確偏移到元素位置的一種方法。它涉及透過指定量移動或轉換元素的座標。當您想要微調文字、標記或其他圖形元素的位置時,此型別的變換非常有用。

例如,如果您有一個位於 (2, 10) 的資料點,並且您想在此點的右側和上方稍微顯示一個標籤,則可以使用偏移變換將特定偏移應用於原始座標。這允許您控制元素相對於其在資料中的原始位置的精確位置。

示例

現在,我們建立一個繪圖,並使用“ScaledTranslation”類建立一個偏移變換。“trans”物件是原始資料變換 (ax.transData) 和資料座標中 (0.1, 0.2) 的縮放平移的組合。然後,ax.text() 函式使用此變換後的座標在 (2, 25) 處定位文字,並具有指定的偏移量 -

import matplotlib.pyplot as plt
from matplotlib.transforms import ScaledTranslation

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

# Creating an offset transformation
trans = ax.transData + ScaledTranslation(0.1, 0.2, ax.transData)

# Adding text with the offset transformation
ax.text(0.4, 0.2, 'Text with Offset', transform=trans)

# Setting plot title and labels
ax.set_title('Offset Transform Example')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

plt.show()
輸出

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

Offset Transform
廣告