📜  如何在 Matplotlib 中反转轴?

📅  最后修改于: 2022-05-13 01:54:41.005000             🧑  作者: Mango

如何在 Matplotlib 中反转轴?

在 Matplotlib 中,我们可以使用多种方法反转图形的轴。最常见的方法是对轴对象使用 invert_xaxis() 和 invert_yaxis() 。除此之外,我们还可以对 pyplot 对象使用 xlim() 和 ylim() 以及 axis() 方法。

方法一:使用 invert_xaxis() 和 invert_yaxis() 方法

要反转 X 轴和 Y 轴,我们可以使用 invert_xaxis() 和 invert_yaxis()函数。我们可以使用上述方法反转任一轴或两个轴。

代码:

Python3
# importing numpy and matplotlib
import numpy as np
import matplotlib.pyplot as plt
 
# creating an x sequence
x = np.linspace(5, 15, 35)
 
# equation of a straight line
y = 3*x+4
 
# creating graph space for two graphs
graph, (plot1, plot2) = plt.subplots(1, 2)
 
# plot1 graph for normal axes
plot1.plot(x, y)
plot1.set_title("Normal Plot")
 
# plot2 graph for inverted axes
plot2.plot(x, y)
plot2.set_title("Inverted Plot")
plot2.invert_xaxis()
plot2.invert_yaxis()
 
# display the graph
graph.tight_layout()
plt.show()


Python3
# importing numpy and matplotlib
import numpy as np
import matplotlib.pyplot as plt
 
# creating an x sequence
x = np.linspace(5, 15, 35)
 
# equation of a straight line
y = 3*x+4
 
# creating graph space for two graphs
graph, (plot1, plot2) = plt.subplots(1, 2)
 
# plot1 graph for normal axes
plot1.plot(x, y)
plot1.set_title("Normal Plot")
 
# plot2 graph for inverted axes
plot2.plot(x, y)
plot2.set_title("Inverted Plot")
plt.xlim(max(x), min(x))
plt.ylim(max(y), min(y))
 
# display the graph
graph.tight_layout()
plt.show()


Python3
# importing numpy and matplotlib
import numpy as np
import matplotlib.pyplot as plt
 
# creating an x sequence
x = np.linspace(5, 15, 35)
 
# equation of a straight line
y = 3*x+4
 
# creating graph space for two graphs
graph, (plot1, plot2) = plt.subplots(1, 2)
 
# plot1 graph for normal axes
plot1.plot(x, y)
plot1.set_title("Normal Plot")
 
# plot2 graph for inverted axes
plot2.plot(x, y)
plot2.set_title("Inverted Plot")
plt.axis([max(x), min(x), max(y), min(y)])
 
# display the graph
graph.tight_layout()
plt.show()


输出:

正态图和倒置图(两个轴都倒置)

方法二:使用 xlim() 和 ylim() 方法

xlim() 和 ylim() 也可用于反转图形的轴。它们通常用于分别设置或获取 X 轴和 Y 轴的限制。但是如果我们将轴中的最小值作为上限,将轴中的最大值作为下限,我们可以得到一个恢复的轴。

代码:

蟒蛇3

# importing numpy and matplotlib
import numpy as np
import matplotlib.pyplot as plt
 
# creating an x sequence
x = np.linspace(5, 15, 35)
 
# equation of a straight line
y = 3*x+4
 
# creating graph space for two graphs
graph, (plot1, plot2) = plt.subplots(1, 2)
 
# plot1 graph for normal axes
plot1.plot(x, y)
plot1.set_title("Normal Plot")
 
# plot2 graph for inverted axes
plot2.plot(x, y)
plot2.set_title("Inverted Plot")
plt.xlim(max(x), min(x))
plt.ylim(max(y), min(y))
 
# display the graph
graph.tight_layout()
plt.show()

输出:

正态图和倒置图(两个轴都倒置)

方法三:使用axis()方法

与 xlim() 和 ylim() 类似,axis() 方法也用于设置 X 轴和 Y 轴的最小值和最大值。因此,如果我们将轴中的最小值作为上限,将轴中的最大值作为下限,我们可以得到一个恢复的轴。

代码:

蟒蛇3

# importing numpy and matplotlib
import numpy as np
import matplotlib.pyplot as plt
 
# creating an x sequence
x = np.linspace(5, 15, 35)
 
# equation of a straight line
y = 3*x+4
 
# creating graph space for two graphs
graph, (plot1, plot2) = plt.subplots(1, 2)
 
# plot1 graph for normal axes
plot1.plot(x, y)
plot1.set_title("Normal Plot")
 
# plot2 graph for inverted axes
plot2.plot(x, y)
plot2.set_title("Inverted Plot")
plt.axis([max(x), min(x), max(y), min(y)])
 
# display the graph
graph.tight_layout()
plt.show()

输出: