Python/Matplotlib - Change the relative size of a subplot


Question

I have two plots

import matplotlib.pyplot as plt
plt.subplot(121)
plt.subplot(122)

I want plt.subplot(122) to be half as wide as plt.subplot(121). Is there a straightforward way to set the height and width parameters for a subplot?

1
27
2/22/2011 8:46:15 PM

Accepted Answer

By simply specifying the geometry with “122”, you're implicitly getting the automatic, equal-sized columns-and-rows layout.

To customise the layout grid, you need to get a little more specific. See “Customizing Location of Subplot Using GridSpec” in the Matplotlib docs.

11
2/13/2016 1:46:22 AM

See the grid-spec tutorial:

http://matplotlib.sourceforge.net/users/gridspec.html

Example code:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

f = plt.figure()

gs = gridspec.GridSpec(1, 2,width_ratios=[2,1])

ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])

plt.show()

You can also adjust the height ratio using a similar option in GridSpec


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon