Matplotlib 简明教程
Matplotlib - Reverse Axes
What is Reverse Axes?
在 Matplotlib 中,反向轴指的是更改轴的方向,将其从默认方向翻转过来。该动作通过反转沿着特定轴(通常是 x 轴或 y 轴)的数据顺序来改变图的可视表示。
In Matplotlib reverse axes refers to changing the direction of an axis, flipping it from its default orientation. This action alters the visual representation of the plot by reversing the order of the data along a specific axis usually the x-axis or y-axis.
Reversing X-axis
若要在 Matplotlib 中反转 x 轴,我们可以使用 plt.gca().invert_xaxis() 函数。该方法反转 x 轴的方向,有效地水平翻转了该图。最初由左至右绘制的数据点现在将由右至左显示。
To reverse the x-axis in Matplotlib we can use the plt.gca().invert_xaxis() function. This method inverts the direction of the x-axis effectively flipping the plot horizontally. Data points that were originally plotted from left to right will now be displayed from right to left.
以下是对如何反转 x 轴的详细说明:
Here is a detailed breakdown of how to reverse the x-axis:
Steps to Reverse the X-axis
以下是反转 x 轴所要遵循的步骤。
The below are the steps to be followed to reverse the x-axis.
Create a Plot
使用 Matplotlib 根据我们的数据生成一个图。
Generate a plot with our data using Matplotlib.
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot with default axis orientation
plt.plot(x, y, marker='o')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Default X-axis')
plt.show()
Reverse the X-axis
在创建图之后使用 plt.gca().invert_xaxis() 来反转 x 轴。
After creating the plot use plt.gca().invert_xaxis() to reverse the x-axis.
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot with default axis orientation
plt.plot(x, y, marker='o')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Default X-axis')
plt.show()
# Reversing the x-axis
plt.plot(x, y, marker='o')
plt.gca().invert_xaxis() # Reverse x-axis
plt.xlabel('Reversed X-axis')
plt.ylabel('Y-axis')
plt.title('Reversed X-axis')
plt.show()
第二个图将显示与第一个图相同的数据,但 x 轴将被反转。最初位于左侧的数据点现在将出现在右侧,从而改变数据的可视呈现。
The second plot will display the same data as the first one but the x-axis will be reversed. Data points that were initially on the left side will now appear on the right side by altering the visual representation of the data.
Use Cases for Reversing the X-axis
Flipping Time Series Data − 当我们在绘制时间序列数据时,反转 x 轴可以更好地与时间顺序对齐。
Flipping Time Series Data − When we are plotting time series data then reversing the x-axis might better align with the chronological order.
Reorienting Geographical Plots − 在一些地理图中,反转 x 轴可以匹配预期的方向或惯例。
Reorienting Geographical Plots − In some geographical plots reversing the x-axis could match the expected orientation or conventions.
逆转 x 轴可以提供可视化数据的另一种视角,这样我们就能以不同顺序或方向显示信息,以获得更清晰的见解或更好地适应惯例。
Reversing the x-axis provides an alternative perspective for visualizing data allowing us to present information in a different order or orientation for clearer insights or better alignment with conventions.
此函数通过水平翻转该绘图来逆转 x 轴的路径。从左到右最初绘制的数据点现在将从右到左显示。
This function reverses the direction of the x-axis by flipping the plot horizontally. Data points that were originally plotted from left to right will now be displayed from right to left.
Reversing Y-axis
plt.gca().invert_yaxis() 函数通过垂直翻转该绘图来逆转 y 轴的路径。从底至上最初绘制的数据点现在将从上至下显示。
The plt.gca().invert_yaxis() function reverses the direction of the y-axis by flipping the plot vertically. Data points that were originally plotted from bottom to top will now be displayed from top to bottom.
逆转 y 轴与我们在上一节看到的逆转绘图的 x 轴相同。以下是逆转 y 轴的步骤。
The reversing of Y - axis is also as same as the reversing the X - axis of a plot which we seen in the upper section. The below are the steps to be followed for reversing the Y - axis.
使用 Matplotlib 根据我们的数据生成一个图。
Generate a plot with our data using Matplotlib.
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot with default axis orientation
plt.plot(x, y, marker='o')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Default Y-axis')
plt.show()
创建该绘图后,使用 plt.gca().invert_yaxis() 来逆转 y 轴。
After creating the plot use plt.gca().invert_yaxis() to reverse the y-axis.
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot with default axis orientation
plt.plot(x, y, marker='o')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Default Y-axis')
plt.show()
# Reversing the x-axis
# Reversing the y-axis
plt.plot(x, y, marker='o')
plt.gca().invert_yaxis() # Reverse y-axis
plt.xlabel('X-axis')
plt.ylabel('Reversed Y-axis')
plt.title('Reversed Y-axis')
plt.show()