Matplotlib 简明教程

Matplotlib - Ellipse with Units

椭圆是一种看起来像拉伸的圆的几何形状。其由两个关键属性定义:其主轴(最长直径)及其短轴(最短直径)。这些轴在椭圆的中心相交,椭圆的形状由这些轴的长度决定。从中心到主轴边缘的距离和从中心到短轴边缘的距离都很重要。

在单位方面,您将使用某种单位(如英寸、厘米或您选择的任何其他度量)来测量这些距离。所以,当我们使用单位说明椭圆时,我们的意思是使用特定度量(单位)来描述椭圆的大小,要考虑主轴长度和短轴长度。

ellipse with units1

Ellipse with Units in Matplotlib

我们可以使用“matplotlib.patches”模块中的“Ellipse”类,在 Matplotlib 中创建带有单位的椭圆。Ellipse 类允许您定义椭圆的中心、宽度和高度(主轴和短轴)、旋转角以及椭圆的其他属性,例如其旋转角。

轴的测量单位取决于您使用的坐标系。例如,如果您使用的是英寸中的绘图,则主轴和短轴长度将以英寸为单位。

Fixed Size Ellipse in Data Coordinates

在 Matplotlib 中,创建数据坐标中固定大小的椭圆涉及按数据坐标绘制特定大小和位置的椭圆。这意味着椭圆的尺寸按照您实际数据的单位指定。

例如,如果你的数据集具有 x 和 y 值,你可以绘制一个椭圆,其中心位于特定点 (x, y) 上,而椭圆的宽度和高度则依照数据坐标来定义。

Example

我们在数据坐标中绘制一个固定尺寸的简单椭圆。椭圆的中心位于图上的 (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()

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

ellipse with units2

Ellipse with Variable Size in Axes Coordinates

在 Matplotlib 中以轴坐标创建可变大小的椭圆包括在图上绘制一个椭圆,其中维度是根据轴而不是实际数据来指定的。换句话说,椭圆的宽度和高度分别给出为 x 轴和 y 轴总长度的百分比。

例如,如果你想绘制一个椭圆,它总是覆盖 x 轴的 60% 和 y 轴的 30%,而不考虑特定的数据值,则可以使用轴坐标。当你想让椭圆的大小相对于图的整体维度时,这一点尤其有用。

Example

在这里,我们绘制了一个轴坐标中指定的可变大小的椭圆。椭圆的中心放置在 (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()

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

ellipse with units3

Ellipse Centered at Figure Origin

在 Matplotlib 中创建以图形原点为中心的椭圆包括在图上绘制一个椭圆,其中心位于整个图形的原点上。原点是图形坐标系中的点 (0, 0)。在这种情况下,椭圆的宽度和高度以图形的单位指定,并且椭圆的中心恰好在原点上。

这演示了如何相对于整体图形而不是各个轴放置椭圆。

Example

在下面的示例中,我们在图形的原点 (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 with units4

Rotated Ellipse with Custom Angle

在 Matplotlib 中创建具有自定义角度的旋转椭圆包括在图上绘制一个椭圆,并指定一个自定义角度来旋转它。旋转角度决定了椭圆在图中如何倾斜或旋转。当你想突出显示特定方向中的特征时,这很有用。

Example

现在,我们创建了一个旋转椭圆,其自定义角度为“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()

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

ellipse with units5