使用 matplotlib 时,如何将 x 轴和 y 轴的屏幕长度设置为相等而不更改任一轴的限制?

问题描述 投票:0回答:1

我想让 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帖子中显示和描述的结果。 一般来说,结果图的轴不是方形的。

python matplotlib axis
1个回答
0
投票

如果我理解正确,那么我认为您正在寻找的是

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()

© www.soinside.com 2019 - 2024. All rights reserved.