Matplotlib 简明教程

Matplotlib - Transforms

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

Transform in general is a way to change something from one form to another. It is like taking an input and turning it into a different output. Transforms are used in various fields, such as mathematics, physics, and computer science.

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

For example, in mathematics, a transform can be a mathematical operation that changes a set of numbers or functions into a different representation. The Fourier transform, for instance, converts a function in the time domain (like a sound wave) into its frequency components.

transforms1

Transforms in Matplotlib

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

In Matplotlib, transforms refer to the conversion process from data coordinates to pixel coordinates, allowing to place the graphical elements such as points, lines, and text within a plot accurately.

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

You can use various types of transformations in matplotlib, such as linear transformations, logarithmic transformations, and more. To handle these transformations, Matplotlib provides the "transform" parameter.

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

The "transform" parameter can take various values, such as instances of the "matplotlib.transforms.Transform" class or strings representing common transformations like 'ax.transData' for data coordinates or 'ax.transAxes' for axes coordinates. By using the transform parameter, you can customize the coordinate system and apply different transformations to enhance the visualization of their data on the Matplotlib plots.

Data to Axes Transform

在 Matplotlib 中,数据到坐标轴变换是指将实际数据点转换为绘图坐标轴内的坐标的过程。当绘制数据时,您会使用具体的“x”和“y”值。该转换确保了这些数据点在您的绘图的坐标轴内正确放好位置,同时考虑“x”和“y”轴的缩放和限制。

In Matplotlib, the data to axes transform refers to the process of converting your actual data points into coordinates within the axes of your plot. When you plot data, you use specific "x" and "y" values. The transformation ensures that these data points are correctly positioned within the axes of your plot, taking into account the scaling and limits of the "x" and "y" axes.

在下面这个示例中,我们正在使用数据坐标(x,y)创建简单的线条绘图。然后我们使用 ax.text() 函数在绘图中添加文本,具体指定了数据坐标中的位置(2,25)。“transform=ax.transData”参数确保文本使用数据坐标来定位 −

In the following example, we are creating a simple line plot using data coordinates (x, y). We then use the ax.text() function to add text to the plot, specifying the position (2, 25) in data coordinates. The "transform=ax.transData" parameter ensures that the text is positioned using data coordinates −

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

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

Following is the output of the above code −

transforms2

Axes to Data Transform

Matplotlib 中的坐标轴到数据转换是“数据到坐标轴转换”的逆。虽然“数据到坐标轴转换”将实际数据点转换为绘图坐标轴内的坐标,“坐标轴到数据转换”则相反。它采用根据绘图的坐标轴指定好的坐标,然后将其转换为对应的数据值。

The axes to data transform in Matplotlib is the reverse of the "data to axes transform". While the "data to axes transform" converts your actual data points into coordinates within the axes of your plot, the "axes to data transform" does the opposite. It takes coordinates specified in terms of the axes of your plot and converts them into the corresponding data values.

在这里,我们创建了一个绘图,对于“x”和“y”轴,轴限制均设置为“0”和“1”之间。我们使用 ax.text() 函数在绘图中添加文本,具体指定了坐标轴坐标中的位置(“0.5, 0.5”)。“transform=ax.transAxes”参数确保文本位置解释为相对于坐标轴而非数据 −

In here, we are creating a plot with axis limits set between "0" and "1" for both "x" and "y" axes. We use the ax.text() function to add text to the plot, specifying the position "(0.5, 0.5)" in axes coordinates. The "transform=ax.transAxes" parameter ensures that the text position is interpreted as relative to the axes rather than the data −

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

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

On executing the above code we will get the following output −

transforms3

Blended Transform

在 Matplotlib 中,混合变换是一种在绘图时放置元素时组合或混合不同坐标系的方法。它允许您创建定制的变换,其中包含数据坐标和坐标轴坐标这二者的一些部分。

In Matplotlib, a blended transform is a way to combine or blend different coordinate systems when placing elements on a plot. It allows you to create a custom transformation that contains aspects of both data coordinates and axes coordinates.

在下面的示例中,我们创建一个绘图,然后使用 blended_transform_factory() 函数创建混合变换。将来的“trans”对象是数据坐标和坐标轴坐标的混合结果。然后 ax.text() 函数使用此混合变换在 (0.8,0.2) 处定位文本,有效地组合了数据坐标和坐标轴坐标 −

In the example below, we are creating a plot and using the blended_transform_factory() function to create a blended transformation. The resulting "trans" object is a blend of both data and axes coordinates. The ax.text() function then uses this blended transformation to position text at (0.8, 0.2), effectively combining data and axes coordinates −

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

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

After executing the above code, we get the following output −

transforms4

Offset Transform

在 Matplotlib 中,偏移变换是向绘图中元素的位置添加精确偏移的方法。它包括通过指定好的距离来移动或转换元素的坐标。当您想要微调文本、标记或其他图表元素的放置时,这种类型的转换非常有用。

In Matplotlib, an offset transform is a way to add a precise offset to the position of elements on a plot. It involves shifting or translating the coordinates of the elements by a specified amount. This type of transformation is useful when you want to fine-tune the placement of text, markers, or other graphical elements.

例如,如果您在(2,10)处有一个数据点,并且您希望在该点的右侧以及上方稍微偏离一点处显示一个标签,您可以使用偏移变换对原始坐标应用特定的偏移。这允许您相对于它们在数据中的原始位置来控制元素的确切放置。

For example, if you have a data point at (2, 10) and you want to display a label slightly to the right and above this point, you can use an Offset Transform to apply a specific offset to the original coordinates. This allows you to control the exact positioning of elements relative to their original locations in the data.

现在,我们创建一个绘图,然后使用“ScaledTranslation”类来创建偏移变换。“trans”对象是原始数据变换(ax.transData)和数据坐标中的比例变换(0.1,0.2)的组合。然后 ax.text() 函数使用此变换后的坐标将文本定位在(2,25)处,偏移量为指定值 −

Now, we are creating a plot, and using the "ScaledTranslation" class to create an offset transformation. The "trans" object is a combination of the original data transformation (ax.transData) and a scaled translation of (0.1, 0.2) in data coordinates. The ax.text() function then uses this transformed coordinate to position text at (2, 25) with the specified offset −

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

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

On executing the above code we will get the following output −

transforms5