Matplotlib 简明教程
Matplotlib - Axis Ranges
What is Axis Range?
Matplotlib 中的轴范围是指沿绘图中的 X 轴和 Y 轴显示值跨度。这些范围确定绘图区域内可见的数据,并由每个轴上显示的最小值和最大值定义。
Axis range in Matplotlib refers to the span of values displayed along the x-axis and y-axis in a plot. These ranges determine the visible data within the plot area and are defined by the minimum and maximum values displayed on each axis.
在 Matplotlib 中自定义轴范围允许更好地控制绘图中可见的数据,通过强调特定范围或模式来增强可视化。
Customizing axis ranges in Matplotlib allows for better control over the visible data within the plot by enabling the emphasis of specific ranges or patterns to enhance the visualization.
The Key Points about Axis Ranges
X-axis and Y-axis Ranges − 可以独立调整 X 轴和 Y 轴的轴范围,以显示数据的特定部分。
X-axis and Y-axis Ranges − Axis ranges can be adjusted independently for the x-axis and y-axis to display specific portions of the data.
Setting Ranges − 使用如 plt.xlim() 和 plt.ylim() 这样的函数或它们同等的方法,可以手动设置各轴范围,或者使用轴对象 ax.set_xlim() 和 ax.set_ylim() 。
Setting Ranges − We can manually set the axis ranges using functions like plt.xlim() and plt.ylim() or their equivalent methods when working with an axis object ax.set_xlim() and ax.set_ylim().
Automatic Ranges − 默认情况下,Matplotlib 库将根据提供的数据自动计算和设置各轴范围。但手动调整可将注意力集中到特定数据范围内,或提高可视化效果。
Automatic Ranges − By default the Matplotlib library automatically calculates and sets the axis ranges based on the data provided. However manual adjustment allows focusing on specific data ranges or enhancing visualization.
Use Cases for Axis Ranges
Zooming In or Out − 调整各轴范围,可通过在绘图中放大或缩小,将注意力集中在数据特定部分上。
Zooming In or Out − Adjusting axis ranges to focus on specific parts of the data by zooming in or out within the plot.
Data Emphasis − 突出显示数据特定范围,以强调模式或趋势。
Data Emphasis − Highlighting specific ranges of the data to emphasize patterns or trends.
Avoiding Clutter − 我们可以调整绘图轴范围,防止数据点重叠,从而提高绘图中某些部分的可视化效果。
Avoiding Clutter − We can adjust the ranges of the plot axis, to prevent the overlapping of data point to improve the visualization of certain sections of the plot.
Key Concepts in Axis Range
以下是对绘图轴范围的关键概念。让我们详细地逐一了解它们。
The following are the key concepts of the axis range of the plot. Let’s see each of them in detail.
X-Axis Range
轴范围中的 x 范围是指绘图中沿 x 轴显示的值跨度。它通过定义该维度中显示的数据范围,确定水平轴上可见的最小值和最大值。
The x-range in axis range refers to the span of values displayed along the x-axis in a plot. It determines the minimum and maximum values visible on the horizontal axis by defining the range of data shown in that dimension.
在 Matplotlib 库中,我们可以使用 plt.xlim() 或 ax.set_xlim() 设置 x 轴范围,为 x 轴指定界限。这允许我们控制沿 x 轴显示的数据范围。
In Matplotlib library we can set the x-axis range using plt.xlim() or ax.set_xlim() to specify the limits for the x-axis. This allows us to control the displayed range of data along the x-axis.
通过控制轴范围中的 x 范围,可以灵活地沿着 x 轴可视化数据的特定区段,从而更好地诠释和分析绘图信息。
Controlling the x-range in axis ranges provides flexibility in visualizing specific segments of data along the x-axis by enabling better interpretation and analysis of the plotted information.
在这个例子中 plt.xlim(1, 5) 将 x 轴界限设置为 1 到 5,通过定义绘图中可见的 x 范围,使其涵盖沿 x 轴的 1 到 5 之间的数据点。
In this example plt.xlim(1, 5) sets the x-axis limits from 1 to 5 by defining the x-range visible in the plot to cover data points between 1 and 5 along the x-axis.
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()
以下是一个演示使用 ax.set_xlim() 设置 x 轴界限的示例,它使用的是轴对象 ax 。
Here’s an example demonstrating the use of ax.set_xlim() to set the x-axis limits when working with an axis object 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()
Y-Axis Range
在 Matplotlib 中设置 y 轴范围或界限,允许我们指定沿绘图垂直 y 轴显示的值范围。我们可以控制 y 轴上显示的最小值和最大值,以关注特定数据范围或模式。在 Matplotlib 中,可以使用 plt.ylim() 函数或 ax.set_ylim() 方法设置 y 轴的范围或界限,当使用轴对象 ax 时。沿垂直 y 轴显示的值范围。
Setting the y-axis range or limits in Matplotlib allows us to specify the range of values displayed along the vertical y-axis of a plot. We can control the minimum and maximum values shown on the y-axis to focus on specific data ranges or patterns. The setting of range or limits of the y-axis in Matplotlib can be done using the plt.ylim() function or ax.set_ylim() method when working with an axis object ax. The range of values displayed along the vertical y-axis.
设置 y 轴范围有助于关注特定数据范围,突显趋势,并确保可视化重点突出沿 y 轴的数据相关部分。
Setting y-axis range is beneficial for focusing on specific data ranges, highlighting trends and ensuring the visualization emphasizes the relevant parts of the data along the y-axis.
在这个例子中, plt.ylim(0, 12) 函数将 y 轴界限设置为 0 到 12,通过指定 y 轴上显示的值范围。
In this example the plt.ylim(0, 12) function sets the y-axis limits from 0 to 12 by specifying the range of values displayed on the y-axis.
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 时,我们可以根据要求为特定子图设置 y 轴界限。
When working with an axis object ax we can set the y-axis limits for a specific subplot as per our requirement.
在这个例子中, ax.set_ylim(0, 20) 将 y 轴界限设置为 0 到 20,专门针对子图或轴 ax。
In this example ax.set_ylim(0, 20) sets the y-axis limits from 0 to 20 specifically for the subplot or axis 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()
Change the range of the X-axis and Y-axis
此示例更改 X 和 Y 轴范围,我们可以使用 xlim() 和 ylim() 方法。
This example changes the range of X and Y axes, we can use xlim() and ylim() methods.
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()