简体   繁体   中英

Python: set aspect ratio of figure to 1

How do I get a figure to have a 1:1 aspect ratio? I currently have the following figure

import matplotlib.pyplot as plt

circle1 = plt.Circle((0.5, 0.5), 0.2, color='r')
fig, ax = plt.subplots()
ax.add_artist(circle1)

But the x-axis is bigger than the y-axis. I tried using the command I found here :

import matplotlib.pyplot as plt

circle1 = plt.Circle((0.5, 0.5), 0.2, color='r')
fig, ax = plt.subplots()
ax.add_artist(circle1)

plt.axes().set_aspect('equal', 'datalim')

but then the circle I drew disappears.

How can I set an equal aspect ratio?

Add the aspect kw to your fig, ax statement:

fig, ax = plt.subplots(subplot_kw={'aspect': 1})

If you want to change the aspect ratio of your ax at any time rather than when it's created, you can call:

ax.set_aspect("equal")

as documented here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM