[英]How to equalize the scales of x-axis and y-axis in matplotlib?
I wish to draw lines on a square graph.我想在方图上画线。
The scales of x-axis
and y-axis
should be the same. x-axis
和y-axis
的刻度应该相同。
eg x ranges from 0 to 10 and it is 10cm on the screen.例如,x 的范围从 0 到 10,它在屏幕上是 10cm。 y has to also range from 0 to 10 and has to be also 10 cm. 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')
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()
plt.axis('scaled')
对我来说效果很好。
See the documentation on plt.axis()
.请参阅有关plt.axis()
的文档。 This:这:
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.这将创建一个具有相等轴的方形图。
Try something like:尝试类似:
import pylab as p
p.plot(x,y)
p.axis('equal')
p.show()
您可以使用此将绘图拉伸为正方形:
fig = plt.figure(figsize=(1, 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.