我想让 matplotlib 的轴绘制出正方形,并通过拉伸或压缩轴的缩放而不是更改轴限制来实现。 这怎么办?
(换句话说,我正在寻找 MATLAB 命令的 Python 模拟
axis square
。)
到目前为止,我已经尝试过:
plt.gca().set_aspect('equal')
。 这似乎均衡了 x 轴和 y 轴的缩放比例,而不是屏幕上的长度,与 matplotlib documentation 一致。 结果图的轴不是方形的,除非 ymax - ymin = xmax - xmin,其中 xmin、xmax、ymin 和 ymax 是轴限制(并且此等式不适用于我的图)。
plt.axis('equal')
和plt.axis('scaled')
。 这些似乎提供了this帖子中显示和描述的结果。 一般来说,结果图的轴不是方形的。
ax.set_box_aspect(1)
(另见here),这将为您提供与原始图具有相同限制的方轴。
示例:
import numpy as np
import matplotlib.pyplot as plt
plt.close("all")
x = np.linspace(0, 50, 200)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_box_aspect(1)
fig.tight_layout()