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

本文探讨了如何在使用matplotlib绘制图表时保持x轴和y轴的相同比例,即使在调整窗口大小时也能确保图形保持正方形的比例不变。通过设置plt.gca().set_aspect('equal', adjustable='box')实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I wish to draw lines on a square graph.

The scales of x-axis and y-axis should be the same.

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.

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:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值