简体   繁体   中英

Aspect ratio of a plot

I try to change my aspect ratio in python. I know this has been discussed in many other cases (eg with figaspect), but I don't get it like I want to have it. If I use figaspect, I get the error:

'Figure' object is not callable .

Without it I have this code:

fig, ax = plt.subplots()

ax.plot(y_test1, linewidth=0.5, linestyle="-", label='Test1')
ax.plot(y_test2, linewidth=0.5, linestyle="-", label='Test2') 
legend = ax.legend(loc='right center',prop={'size': 5}, shadow=True, fontsize='medium')
plt.xlabel('images')
plt.ylabel('error')
plt.grid()
plt.axis([0,100, 0, 20])

It works, but I need an other aspect ratio. How do I get a plot that is three times wider than high?

There are a few options. As suggested in the comments, you can increase the figure size manually, making sure the width is three time bigger than the height.

fig, ax = plt.subplots(figsize=(height, height*3))

If you want to use figaspect you can do the following:

from matplotlib.figure import figaspect

w, h = figaspect(1/3)
fig, ax = plt.subplots(figsize=(w,h))

plt.show()

This creates a figure which is 3 times wider than it is tall and gives:

在此处输入图片说明

You can also change the axes aspect ratio using ax.set_aspect() . This will not change the figure size:

fig, ax = plt.subplots()

ax.set_aspect(1/3)

plt.show()

Which gives:

在此处输入图片说明

I think you're best helped with something like this:

ax.set_aspect('auto')

You can find the documentation here This, however, changes the axis aspect ratio. as @MaxPowers points out, you can also alter the figsize of your subplots.

you can specify the figure size directly to produce varied sizes as wanted.

fig, axs = plt.subplots(2, 3, figsize=(10, 3), sharex=True, sharey=True, constrained_layout=True)

Hope this could help.

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