Matplotlib - Axes 类



在 Matplotlib 的上下文中,axes并非指轴的复数形式。相反,它代表图形或画布上的整个绘图区域。其中包括 x 轴、y 轴、绘图数据、刻度、刻度标签等等。

请参考下图:

Axes_Class_intro

考虑一个图形,其中使用 ax = fig.subplots() 方法创建了两个 Axes 对象。第一个 axes 显示指数数据,而第二个 axes 显示正弦波。每个 Axes(子图)都有自己的一组标签、刻度和图例,在同一图形中提供不同的表示。

Matplotlib 中的 Axes 类

Axes() 类是创建数据可视化的入口。一旦在图形上实例化了一个 Axes 对象,就可以使用各种方法在该绘图区域中添加和操作数据。

此类是matplotlib.axes模块的一部分,提供使用 Matplotlib 面向对象编程 (OOP) 接口工作的基本功能。大多数重要的绘图方法都在 Axes 类中定义,使其成为自定义和增强可视化的核心组件。

创建 Axes

创建 Axes 对象通常是 Matplotlib 绘图的第一步。这可以通过 Figure 对象上的方法(如Figure.subplots()Figure.add_axes())或通过 pyplot 接口函数pyplot.subplots()来完成。这些方法可以创建一个或多个 Axes 对象。

示例

以下示例使用pyplot.subplot()方法在一个图形上创建两个 axes。subplots() 方法用于生成 axes 实例。

import matplotlib.pyplot as plt
import numpy as np

# Creating a 1x2 subplot layout
fig, (axes1, axes2)  = plt.subplots(1, 2, figsize=(7, 4),
   layout="constrained")

# Adding labels to each subplot
axes1.annotate('axes1', (0.5, 0.5),transform=axes1.transAxes,
   ha='center', va='center', fontsize=18,
   color='darkgrey')

axes2.annotate('axes2', (0.5, 0.5),transform=axes2.transAxes,
   ha='center', va='center', fontsize=18,
   color='darkgrey')

fig.suptitle('Creating Two Axes on a Figure')

# Displaying the plot
plt.show()

输出

执行上述代码后,将得到以下输出:

axes_class_ex1

更改 Axes 属性

要设置 axes 的属性,必须访问 axes 对象,然后可以使用各种 `set_*` 方法来修改其属性。

示例

import matplotlib.pyplot as plt
import numpy as np

# Creating a 1x2 subplot layout
fig, (axes1, axes2) = plt.subplots(1, 2, figsize=(7, 4),
   constrained_layout=True)

# Changing the properties of the first axes
axes1.set_xlabel("X-axis")        # Set label for X-axis
axes1.set_ylabel("Y-axis")        # Set label for Y-axis
axes1.set_facecolor('lightgreen') # Setting background color
axes1.annotate('axes1', (0.5, 0.5), transform=axes1.transAxes,
   ha='center', va='center', fontsize=18,
   color='darkgrey')
axes2.set_title('Second axes')
axes2.annotate('axes2', (0.5, 0.5), transform=axes2.transAxes,
   ha='center', va='center', fontsize=18,
   color='darkgrey')

# Adding a title to the figure
fig.suptitle('Changing Axes Properties')

# Displaying the plot
plt.show()

输出

执行上述代码后,将得到以下输出:

axes_class_ex4

在 Axes 上绘图

此类提供了几种高级绘图方法,用于在 axes 上创建不同的图。

示例

以下示例使用Axes.plot()方法创建一个表示 sin(x) 的线图。

import matplotlib.pyplot as plt
import numpy as np

# Sample Data
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x**2)

# Create subplots
fig, axs = plt.subplots(figsize=(7,4))

# Draw the plot
axs.plot(x, y)

# Show the plot
plt.show()

输出

执行上述代码后,将得到以下输出:

axes_class_ex2

自定义 axes 数据

Axes 对象包含大部分图形元素,例如 Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。可以通过添加标签、标题、图例和注释来自定义这些元素,从而提高可视化的清晰度。

示例

这是一个简单的示例,它向 Axes 添加标签、标题和图例。

import matplotlib.pyplot as plt
import numpy as np

# Sample Data
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x**2)

# Create subplots
fig, axs = plt.subplots(figsize=(7,4))

# Draw the plot
axs.plot(x, y, label='Sin(x)')

# Add titles
axs.set_title('Sin Plot')

# Add X and Y labels
axs.set_xlabel('X-axis')
axs.set_ylabel('Y-axis')

# Add legend
axs.legend()

# Show the plot
plt.show()

输出

执行上述代码后,将得到以下输出:

axes_class_ex3

清除 Axes

要清除 axes 的内容,可以使用axes.cla()axes.clear()方法。

示例

以下示例演示如何在子图中清除第一个 axes。

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(-1, 1, 10)
y = np.exp(x)

# Creating subplots
fig, (axes1, axes2) = plt.subplots(1, 2, figsize=(7, 4),
   constrained_layout=True)

# Plotting on the first axes
axes1.plot(x, y, c='red')
axes1.set_xlabel("X-axis")        # Set label for X-axis
axes1.set_ylabel("Y-axis")        # Set label for Y-axis
axes1.set_facecolor('lightgreen') # Setting background color
axes1.annotate('axes1', (0.5, 0.5), transform=axes1.transAxes,
   ha='center', va='center', fontsize=18,
   color='darkgrey')

# Adding a title to the second axes
axes2.set_title('Second axes')
axes2.annotate('axes2', (0.5, 0.5), transform=axes2.transAxes,
   ha='center', va='center', fontsize=18,
   color='darkgrey')

# Clearing the first axes
axes1.cla()

# Adding a title to the figure
fig.suptitle('Clearing the Axes')

# Displaying the plot
plt.show()

输出

执行上述代码后,将得到以下输出:

axes_class_ex5
广告
© . All rights reserved.