69

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?

7 Answers 7

79

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.

Sign up to request clarification or add additional context in comments.

4 Comments

What does that fig.add_subplot(111) indicate? I mean the number within the square brackets ((111))
My answer (stackoverflow.com/a/10448306/1273751) works for showing and saving
what does dpi=100 do?
dpi controls the dots per inch. If you are putting the plot on a powerpoint or something, then you would want to increase the dpi for clarity. Alternatively, you could save it with vector graphics.
22

Another way of doing this is using the matplotlib tight_layout function

import matplotlib.pyplot as plt
fig,(ax) = plt.subplots(figsize=(8,4), ncols=1)
data = [0,1,2,3,4]
ax.plot(data)
fig.tight_layout()
fig.show()

Comments

18

just use aspect='auto' when you call imshow

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

X = 10*np.random.rand(5,3)
plt.imshow(X, aspect='auto')

it works even if it is just for showing and not saving

1 Comment

this resizes the images instead of the figure
8

you can try using axis('scaled')

import matplotlib.pyplot as plt
import numpy

#some dummy images
img1 = numpy.array([[.1,.2],[.3,.4]]) 
img2 = numpy.array([[.1,.2],[.3,.4]])

fig,ax = plt.subplots()
ax.imshow(img1,extent=[0,1,0,1])
ax.imshow(img2,extent=[2,3,0,1])
ax.axis('scaled') #this line fits your images to screen 
plt.show()

Comments

4

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.

2 Comments

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.
I think the correct command is .set_size_inches without the fig
3

Also possible to use ax.autoscale with ax object

ax.autoscale(enable=True) 

1 Comment

Hm, enable=True is already the default value...
0

Use compressed layout - https://matplotlib.org/stable/users/explain/axes/constrainedlayout_guide.html#compressed-layout

fig, axs = plt.subplots(2, 2, layout='compressed')

It resizes the entire figure to remove redundant white space.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.