lanselibai
lanselibai

Reputation: 1273

How to change the subplot width/height ratio using matplotlib.pyplot?

If the following code is run

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
a=[[1,2,3],[4,4,4]]
gridspec.GridSpec(2,1)
plt.subplot2grid((2,1), (0,0), colspan=2, rowspan=1)
plt.figure(figsize=(10, 5))
plt.imshow(a)
plt.savefig('fig.png',bbox_inches='tight')

I got the picture below. How to set the width/height ratio of subplot as 1:2, so it can be longer?

enter image description here

Basically, how to do this: enter image description here

Upvotes: 2

Views: 1587

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339230

By default matplotlib shows images with a 1:1 aspect ratio, i.e. each pixel is as wide as it is high. Aspect is defined as height/width. You can set aspect=2 to get an image with pixels twice as heigh as wide.

plt.imshow(a, aspect=2)

Upvotes: 3

Related Questions