如何确保绘图的x和y轴大小相等?

问题描述:

我想使xy轴的长度相等( 地线减去图例应为正方形).我希望在外面绘制图例(我已经能够将图例放在框外).数据(x_max - x_min)中x axis的跨度与数据(y_max - y_min)中y axis的跨度不同.

I want to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ). I wish to plot the legend outside (I have already been able to put legend outside the box). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min).

这是我目前所拥有的代码的相关部分:

This is the relevant part of the code that I have at the moment:

plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 )
plt.axis('equal')
plt.tight_layout()

以下链接是我得到的输出图解的示例:图解

The following link is an example of an output plot that I am getting : plot

我该怎么做?

plt.axis('scaled')将成为您的追随者吗?如果数据限制相等,那将产生一个正方形图.

Would plt.axis('scaled') be what you're after? That would produce a square plot, if the data limits are of equal difference.

如果不是,则可以通过将轴的长宽比设置为xlimits和ylimits的比例来获得平方图.

If they are not, you could get a square plot by setting the aspect of the axes to the ratio of xlimits and ylimits.

import numpy as np
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1,2)

ax1.plot([-2.5, 2.5], [-4,13], "s-")
ax1.axis("scaled")

ax2.plot([-2.5, 2.5], [-4,13], "s-")
ax2.set_aspect(np.diff(ax2.get_xlim())/np.diff(ax2.get_ylim()))

plt.show()