简体   繁体   English

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

[英]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-axisy-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()

doc for set_aspect set_aspect 的文档

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.

相关问题 Matplotlib x轴和副y轴定制问题 - Matplotlib x-axis and secondary y-axis customization questions Plot 具有不同的 x 轴和 y 轴使用 matplotlib - Plot with different x-axis and y-axis using matplotlib 如何调整 x 轴的大小并使其与 y 轴不同 Matplotlib - How to resize the x-axis and make it different from the y-axis Matplotlib 如何在x轴上使用日期并在y轴上使用降雨量来使用matplotlib过滤CSV? - How to filter csv with matplotlib using dates on the x-axis and rainfall on the y-axis? 如何在 matplotlib 中设置自定义 x 轴和 y 轴刻度? - How to set custom x-axis and y-axis ticks in matplotlib? plot matplotlib 如何在 x 轴和 y 轴上颤动箭头? - Python - How to plot matplotlib quiver arrows over x-axis and y-axis? - Python 如何使用 plot 在 x 轴上循环变量 i 和在 y 轴上循环中的局部变量使用 matplotlib - How to plot loop variable i on x-axis and local variable in the loop on y-axis using matplotlib 如何使plot的x轴原点和y轴原点在matplotlib中重叠? - How can I make the origin of the x-axis and the origin of the y-axis of a plot overlap in matplotlib? 在python中具有不同x轴和y轴刻度的两个(或更多)图 - two (or more) graphs in one plot with different x-axis AND y-axis scales in python 如何更改matlibplot中x轴和y轴的范围? - How to change the range of the x-axis and y-axis in matlibplot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM