ChrisGuest ChrisGuest - 6 months ago 34
Python Question

Specify subplot size instead of figsize in matplotlib

I am using matplotlib to draw some graphs. Usually, standard practice is to start with a Figure's dimensions and specify the axes' dimensions so that the graph is then scaled to the available space.

However I have a request to extend the height of a graph to accommodate outlying data on the y axis.

I know that I can use

Figure.figsize
to specify overall dimensions, but this won't keep the the graphs in proportion once a margin has been included. Is there a way to specify the actual subplot size or conversely to determine the margin size for the default margin size and adjust the Figure's dimensions accordingly?

Answer

You mean something like this? You need to calculate margins.

import matplotlib.pyplot as plt

subplotsize=[5.,5.]
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)
plt.show()
Comments