views:

615

answers:

2

Is there a way to automatically resize a figure to properly fit contained plots in a matplotlib/pylab image?

I'm creating heatmap (sub)plots that differ in aspect ratio according to the data used.

I realise I could calculate the aspect ratio and manually set it, but surely there's an easier way?

A: 

Do you mean changing the size of the image or the area that is visable within a plot?

The size of a figure can be set with Figure.set_figsize_inches. Also the SciPy Cookbook has an entry on changing image size which contains a section about multiple images per figure.

Also take a look at this question.

wierob
I mean the dimensions of the Figure object. i.e. Crop the figure to fit the contained plot(s). One would probably have to provide either a width or a height first, and have the other calculated.
pufferfish
A: 

Use bbox_inches='tight'

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

X = 10*np.random.rand(5,3)

fig = plt.figure(figsize=(15,5),facecolor='w') 
ax = fig.add_subplot(111)
ax.imshow(X, cmap=cm.jet)

plt.savefig("image.png",bbox_inches='tight',dpi=100)

...only works when saving images though, not showing them.

pufferfish