1

I am trying to format a figure I am making. I have been trying to set the aspect ratio, but whenever I include the aspect ratio call, or use ax.set_aspect(), I end up with a flattened figure.

fig, ax = plt.subplots(1, 1)
plt.rcParams["font.family"] = 'Calibri'

ax.set(xlim=[415, 700], ylim=[0,1.01], aspect=1)

ax.set_xlabel('Wavelength (nm)')
ax.set_ylabel('Normalized Reflectance (a.u.)')

_ = ax.plot(x_lambda[0], y_refl_norm[0], marker='', linestyle='-', color='m')
_ = ax.plot(x_lambda[1], y_refl_norm[1], marker='', linestyle='-', color='b')
_ = ax.plot(x_lambda[2], y_refl_norm[2], marker='', linestyle='-', color='c')
_ = ax.plot(x_lambda[3], y_refl_norm[3], marker='', linestyle='-', color='g')
_ = ax.plot(x_lambda[4], y_refl_norm[4], marker='', linestyle='-', color='y')
_ = ax.plot(x_lambda[5], y_refl_norm[5], marker='', linestyle='-', 
color='orange')
_ = ax.plot(x_lambda[6], y_refl_norm[6], marker='', linestyle='-', color='r')

ax.legend(('30 degrees', '35 degrees', '40 degrees', '45 degrees', '50 
degrees', '55 degrees', '60 degrees'), loc='lower left', fontsize='x-small',
frameon='True', facecolor='navajowhite', framealpha=0.95)

Without the "aspect=1"

With the "aspect=1"

yixingking
  • 11
  • 2

1 Answers1

1

What else would you expect if the range of your x-axis is ~300x larger than the range on the y-axis? With aspect=1 this gives you a figure of which the height is ~300x smaller than the width.

Take this simple example:

import matplotlib.pylab as pl

pl.figure()
pl.subplot(231, aspect=1)
pl.fill_between([0,1],[0,0],[1,1])
pl.xlim(0,10)
pl.ylim(0,10)

pl.subplot(232, aspect=1)
pl.fill_between([0,1],[0,0],[1,1])
pl.xlim(0,10)
pl.ylim(0,5)

pl.subplot(233, aspect=1)
pl.fill_between([0,1],[0,0],[1,1])
pl.xlim(0,10)
pl.ylim(0,1)

pl.subplot(234)
pl.fill_between([0,1],[0,0],[1,1])
pl.xlim(0,10)
pl.ylim(0,10)

pl.subplot(235)
pl.fill_between([0,1],[0,0],[1,1])
pl.xlim(0,10)
pl.ylim(0,5)

pl.subplot(236)
pl.fill_between([0,1],[0,0],[1,1])
pl.xlim(0,10)
pl.ylim(0,1)

When the x-range is equal to the y-range you end up with a square figure in order to keep the aspect ratio of the x-to-y axis equal (black block = square). If the y-range is half the x-range, the height will be half the width, again to keep the aspect ratio equal.

In your case, the only solution is to use an aspect ratio unequal to one, unless you want to make a veeeeeery wide figure in order to keep the y-axis readable.

enter image description here

Bart
  • 9,825
  • 5
  • 47
  • 73
  • 1
    Thanks! I guess I really misunderstood what "aspect=1" meant, but that makes much more sense that it is the aspect of the data rather than the aspect ratio of the printed figure. – yixingking Oct 27 '17 at 19:19
  • err aspect ratio of the axes* – yixingking Oct 27 '17 at 19:26
  • It is quite easy to set the aspect ratio of the figure, see e.g. https://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib – Bart Oct 27 '17 at 19:53