Matplotlib 简明教程

Matplotlib - Axis Ranges

What is Axis Range?

Matplotlib 中的轴范围是指沿绘图中的 X 轴和 Y 轴显示值跨度。这些范围确定绘图区域内可见的数据,并由每个轴上显示的最小值和最大值定义。

在 Matplotlib 中自定义轴范围允许更好地控制绘图中可见的数据,通过强调特定范围或模式来增强可视化。

The Key Points about Axis Ranges

X-axis and Y-axis Ranges − 可以独立调整 X 轴和 Y 轴的轴范围,以显示数据的特定部分。

Setting Ranges − 使用如 plt.xlim()plt.ylim() 这样的函数或它们同等的方法,可以手动设置各轴范围,或者使用轴对象 ax.set_xlim()ax.set_ylim()

Automatic Ranges − 默认情况下,Matplotlib 库将根据提供的数据自动计算和设置各轴范围。但手动调整可将注意力集中到特定数据范围内,或提高可视化效果。

Use Cases for Axis Ranges

Zooming In or Out − 调整各轴范围,可通过在绘图中放大或缩小,将注意力集中在数据特定部分上。

Data Emphasis − 突出显示数据特定范围,以强调模式或趋势。

Avoiding Clutter − 我们可以调整绘图轴范围,防止数据点重叠,从而提高绘图中某些部分的可视化效果。

Key Concepts in Axis Range

以下是对绘图轴范围的关键概念。让我们详细地逐一了解它们。

X-Axis Range

轴范围中的 x 范围是指绘图中沿 x 轴显示的值跨度。它通过定义该维度中显示的数据范围,确定水平轴上可见的最小值和最大值。

在 Matplotlib 库中,我们可以使用 plt.xlim()ax.set_xlim() 设置 x 轴范围,为 x 轴指定界限。这允许我们控制沿 x 轴显示的数据范围。

通过控制轴范围中的 x 范围,可以灵活地沿着 x 轴可视化数据的特定区段,从而更好地诠释和分析绘图信息。

在这个例子中 plt.xlim(1, 5) 将 x 轴界限设置为 1 到 5,通过定义绘图中可见的 x 范围,使其涵盖沿 x 轴的 1 到 5 之间的数据点。

import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating a plot
plt.plot(x, y)
# Setting x-axis limits (x-range)
plt.xlim(1, 5)  # Set x-axis limits from 1 to 5
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Custom X-axis Range')
plt.show()
pltxlim

以下是一个演示使用 ax.set_xlim() 设置 x 轴界限的示例,它使用的是轴对象 ax

import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating a figure and axis
fig, ax = plt.subplots()
# Plotting on the axis
ax.plot(x, y)
# Setting x-axis limits using axis object
ax.set_xlim(0, 6)  # Set x-axis limits from 0 to 6
# Labeling axes and adding title
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Plot with Custom X-axis Limits')
plt.show()
ax xlim

Y-Axis Range

在 Matplotlib 中设置 y 轴范围或界限,允许我们指定沿绘图垂直 y 轴显示的值范围。我们可以控制 y 轴上显示的最小值和最大值,以关注特定数据范围或模式。在 Matplotlib 中,可以使用 plt.ylim() 函数或 ax.set_ylim() 方法设置 y 轴的范围或界限,当使用轴对象 ax 时。沿垂直 y 轴显示的值范围。

设置 y 轴范围有助于关注特定数据范围,突显趋势,并确保可视化重点突出沿 y 轴的数据相关部分。

在这个例子中, plt.ylim(0, 12) 函数将 y 轴界限设置为 0 到 12,通过指定 y 轴上显示的值范围。

import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating a plot
plt.plot(x, y)
# Setting y-axis limits
plt.ylim(0, 12)  # Set y-axis limits from 0 to 12
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Custom Y-axis Range')
plt.show()
ax ylim

使用轴对象 ax 时,我们可以根据要求为特定子图设置 y 轴界限。

在这个例子中, ax.set_ylim(0, 20) 将 y 轴界限设置为 0 到 20,专门针对子图或轴 ax。

import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating subplots
fig, ax = plt.subplots()
# Plotting on the axis
ax.plot(x, y)
# Setting y-axis limits using axis object
ax.set_ylim(0, 20)  # Set y-axis limits from 0 to 12
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Plot with Custom Y-axis Range')
plt.show()
ax ylim

Change the range of the X-axis and Y-axis

此示例更改 X 和 Y 轴范围,我们可以使用 xlim() 和 ylim() 方法。

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-15, 15, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlim(-10, 10)
plt.ylim(-1, 1)
plt.show()
x y limits