I am learning the matplotlib library. One thing I didn't understand is figsize and fig.add_axes([0,0,x,x]) are both doing the same thing, aren't they?
For example:
fig1 = plt.figure(figsize=(8,2))
ax1 = fig.add_axes([0,0,2,2])
and
fig2 = plt.figure(figsize=(17,4))
ax2 = fig.add_axes([0,0,0.8,0.8])
Both are producing the same plot area. So where is the difference between two?
CodePudding user response:
A figure is a container for axes:
The
figsizeparam sets the outer figure dimensions(width, height)in inches.The
fig.add_axes()method sets the inner axes dimensions[left, bottom, width, height]in fractions offigsize.
So the first example creates an 8×2 outer figure with 16×4 axes (8*2 and 2*2):
fig1 = plt.figure(figsize=(8,2))
ax1 = fig.add_axes([0,0,2,2])
And the second example creates a 17×4 outer figure with 13.6×3.2 axes (17*0.8 and 4*0.8):
fig2 = plt.figure(figsize=(17,4))
ax2 = fig.add_axes([0,0,0.8,0.8])
So the two figures and axes are actually different sizes.
Also note that you probably wouldn't want to use the first example in practice since it creates axes that are larger than the figure container.
