1

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?

DavidG
  • 24,279
  • 14
  • 89
  • 82
csi
  • 230
  • 1
  • 7
  • 22
  • Please give some sample code so we can run it on our machines. thanks – Dorian Jan 10 '18 at 15:24
  • Try `fig, ax = plt.subplots(figsize=(1,3))`. – MaxPowers Jan 10 '18 at 15:29
  • Thanks Max, it works! It looks so easy, I just thought too complicated.. – csi Jan 10 '18 at 16:39
  • Possible duplicate of [Aspect ratio in subplots with various y-axes](https://stackoverflow.com/questions/14907062/aspect-ratio-in-subplots-with-various-y-axes) – jdhao Jan 11 '18 at 00:55
  • Possible duplicate of [How can I set the aspect ratio in matplotlib?](https://stackoverflow.com/questions/7965743/how-can-i-set-the-aspect-ratio-in-matplotlib) – jtlz2 Oct 29 '18 at 09:22

3 Answers3

7

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

DavidG
  • 24,279
  • 14
  • 89
  • 82
1

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.

Dorian
  • 1,439
  • 1
  • 11
  • 26
0

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.

Zachary
  • 179
  • 1
  • 7