如何在 Python matplotlib 中均衡 x 轴和 y 轴的比例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17990845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:41:05  来源:igfitidea点击:

How to equalize the scales of x-axis and y-axis in Python matplotlib?

pythonmatplotlib

提问by Sibbs Gambling

I wish to draw lines on a square graph.

我想在方图上画线。

The scales of x-axisand y-axisshould be the same.

天秤x-axisy-axis应该是相同的。

e.g. x ranges from 0 to 10 and it is 10cm on the screen. y has to also range from 0 to 10 and has to be also 10 cm.

例如,x 的范围从 0 到 10,它在屏幕上是 10cm。y 也必须在 0 到 10 的范围内,并且也必须是 10 cm。

The square shape has to be maintained, even if I mess around with the window size.

即使我弄乱了窗口大小,也必须保持方形。

Currently, my graph scales together with the window size.

目前,我的图形与窗口大小一起缩放。

How may I achieve this?

我怎样才能做到这一点?

UPDATE:

更新:

I tried the following, but it did not work.

我尝试了以下方法,但没有奏效。

plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.axis('equal')

采纳答案by tacaswell

You need to dig a bit deeper into the api to do this:

您需要更深入地研究 api 才能做到这一点:

from matplotlib import pyplot as plt
plt.plot(range(5))
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.gca().set_aspect('equal', adjustable='box')
plt.draw()

doc for set_aspect

set_aspect 的文档

回答by Dman2

Try something like:

尝试类似:

import pylab as p
p.plot(x,y)
p.axis('equal')
p.show()

回答by myx

plt.axis('scaled')

works well for me.

对我来说效果很好。

回答by Adam Stewart

See the documentationon plt.axis(). This:

文档plt.axis()。这个:

plt.axis('equal')

doesn't work because it changes the limits of the axis to make circles appear circular. What you want is:

不起作用,因为它改变了轴的限制,使圆看起来是圆形的。你想要的是:

plt.axis('square')

This creates a square plot with equal axes.

这将创建一个具有相等轴的方形图。