csi
csi

Reputation: 240

Aspect ratio of a plot

I try to change my aspect ratio in python. I know this has been discussed in many other cases (e.g. 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?

Upvotes: 2

Views: 16718

Answers (3)

Zachary
Zachary

Reputation: 307

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.

Upvotes: 0

DavidG
DavidG

Reputation: 25363

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:

enter image description here

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:

enter image description here

Upvotes: 7

Dorian
Dorian

Reputation: 1488

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 figsizeof your subplots.

Upvotes: 1

Related Questions