3

My figure has dimensions of figsize=(10,10) and one subplot within it. How can i define the dimension of the subplot to be 8 inches W x 8 inches H regardles the data range the subplot it shows?

EDIT: The reason I am searching for this is that I am trying to create some plots for publication. So I need my plots to have a fixed width and height to be able to be inserted to the manuscrpipt seamsly.

Here's the striped down code:

map_size = [8,8]
fig, ax = plt.subplots(1,1,figsize=map_size)
ax.plot(data)
fig.savefig('img.png', dpi=300,)  # figure is the desired size, subplot seems to be scaled to fit to fig
user528025
  • 732
  • 2
  • 10
  • 24
  • if the subplot in your figure occupies (80, 80)% of the width and height respectively, is that what you're going for? Or do you want the subplot to occupy 8 inches width and height regardless of the figure size? – ypx May 28 '15 at 07:47
  • 1
    Regardless of the figure size I want if possible the subplot to have a constant size. Of Course the figure is bigger from the subplot to accomodate any labels. – user528025 May 28 '15 at 07:50
  • Possible duplicate of https://stackoverflow.com/questions/24811059/matplotlib-pyplot-subplot-size#24811670 – fouronnes May 28 '15 at 08:07
  • @user528025 One option is to define the subplot as a standalone figure and set its size in inches, then insert as a subplot inside the 'master' figure. [How do I include a matplotlib Figure object as subplot?](http://stackoverflow.com/a/16754215/2466336) – ypx May 28 '15 at 08:14

1 Answers1

4

One option is to use fig.subplots_adjust to set the size of the subplot within the figure. The arguments left, right, bottom and top are fractional units (of the total figure dimensions). So, for an 8x8 subplot in a 10x10 figure, right - left = 0.8, etc.:

import matplotlib.pyplot as plt

fig=plt.figure(figsize=(10.,10.))
fig.subplots_adjust(left=0.1,right=0.9,bottom=0.1,top=0.9)

ax=fig.add_subplot(111)

Doing it like this, you would have to manually change the left, right, bottom and top values if you change the figure size.

I guess you could build that calculation into your code:

import matplotlib.pyplot as plt

subplotsize=[8.,8.]
figuresize=[10.,10.]   

left = 0.5*(1.-subplotsize[0]/figuresize[0])
right = 1.-left
bottom = 0.5*(1.-subplotsize[1]/figuresize[1])
top = 1.-bottom

fig=plt.figure(figsize=(figuresize[0],figuresize[1]))
fig.subplots_adjust(left=left,right=right,bottom=bottom,top=top)

ax=fig.add_subplot(111)
tmdavison
  • 64,360
  • 12
  • 187
  • 165