Matplotlib 简明教程

Matplotlib - Transforms

一般来说,变换是一种将某物从一种形式改成另一种形式的方法。就像获取一个输入并将其转换成一个不同的输出。各种领域都在使用变换,例如数学、物理学和计算机科学。

例如,在数学中,变换可以是一种将一组数字或函数转换成不同表示的数学运算。比如,傅里叶变换将时域中的函数(例如声波)转换为其频率分量。

transforms1

Transforms in Matplotlib

在 Matplotlib 中,变换是指从数据坐标到像素坐标的转换过程,允许准确地将图形元素(比如点、线和文本)放在绘图中。

你可以在 matplotlib 中使用各种类型的变换,例如线性变换、对数变换等等。为了处理这些变换,Matplotlib 提供了“transform”参数。

“transform”参数可以采用各种值,例如“matplotlib.transforms.Transform”类的实例或表示常见变换的字符串,例如“ax.transData”表示数据坐标或“ax.transAxes”表示轴坐标。通过使用变换参数,你可以自定义坐标系,并应用不同的变换来增强在 Matplotlib 绘图中对其数据的可视化效果。

Data to Axes Transform

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

以下是上面代码的输出: -

transforms2

Axes to Data 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()

执行上述代码,我们将得到以下输出 −

transforms3

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

执行上面的代码后,我们得到以下输出: -

transforms4

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

执行上述代码,我们将得到以下输出 −

transforms5