Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Matplotlib - Change the relative size of a subplot

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?

like image 510
thenickname Avatar asked Feb 22 '11 20:02

thenickname


People also ask

How do I change the subplot size in Matplotlib?

To change the size of subplots in Matplotlib, use the plt. subplots() method with the figsize parameter (e.g., figsize=(8,6) ) to specify one size for all subplots — unit in inches — and the gridspec_kw parameter (e.g., gridspec_kw={'width_ratios': [2, 1]} ) to specify individual sizes.

How do I control the size of a subplot in Python?

To change figure size of more subplots you can use plt. subplots(2,2,figsize=(10,10)) when creating subplots.

How do you change the size of a plot in Matplotlib?

Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.


2 Answers

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() 

enter image description here

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

like image 114
JoshAdel Avatar answered Sep 22 '22 18:09

JoshAdel


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.

like image 23
bignose Avatar answered Sep 23 '22 18:09

bignose