简体   繁体   中英

How to change the size of plot with equal aspect ratio?

It seems that I can't have both setting equal axes scales AND setting the size of the plot. What I'm doing is:

fig = pl.figure(figsize=(20,20))
ax = fig.add_subplot(111)
ax.set_aspect('equal')

If I remove the figsize the plot seems to have equal scales, but with figsize I have a bigger plot but the scales aren't equal anymore.

Edit: The graph does not necessarily have to be square, just bigger.. please assume that I don't now the exact ratio of the axes

Any solutions?

Thanks

If you want to change the data limits to make the axes square, add datalim to your call to set_aspect :

ax.set_aspect('equal', 'datalim')

If you want the scaling to change so that the limits are different but the axes look square, you can calculate the axis size ratio and set it explicitly:

ax.set_aspect(1./ax.get_data_ratio())

eg

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111)

x = np.linspace(0,np.pi,1000)
y = np.sin(3*x)**2
ax.plot(x,y)
ax.set_aspect('equal', 'datalim')
plt.show()

在此处输入图片说明

or

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111)

x = np.linspace(0,np.pi,1000)
y = np.sin(3*x)**2
ax.plot(x,y)
ax.set_aspect(1./ax.get_data_ratio())
plt.show()

在此处输入图片说明

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