Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib different size subplots

I need to add two subplots to a figure. One subplot needs to be about three times as wide as the second (same height). I accomplished this using GridSpec and the colspan argument but I would like to do this using figure so I can save to PDF. I can adjust the first figure using the figsize argument in the constructor, but how do I change the size of the second plot?

like image 202
Jason Strimpel Avatar asked Apr 30 '12 18:04

Jason Strimpel


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 change the plot size 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.

What is Matplotlib Figsize?

Matplotlib allows the aspect ratio, DPI and figure size to be specified when the Figure object is created, using the figsize and dpi keyword arguments. figsize is a tuple of the width and height of the figure in inches, and dpi is the dots-per-inch (pixel per inch).


2 Answers

  • Another way is to use the subplots function and pass the width ratio with gridspec_kw
    • matplotlib Tutorial: Customizing Figure Layouts Using GridSpec and Other Functions
    • matplotlib.gridspec.GridSpec has available gridspect_kw options
import numpy as np import matplotlib.pyplot as plt   # generate some data x = np.arange(0, 10, 0.2) y = np.sin(x)  # plot it f, (a0, a1) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]}) a0.plot(x, y) a1.plot(y, x)  f.tight_layout() f.savefig('grid_figure.pdf') 

enter image description here

  • Because the question is canonical, here is an example with vertical subplots.
# plot it f, (a0, a1, a2) = plt.subplots(3, 1, gridspec_kw={'height_ratios': [1, 1, 3]})  a0.plot(x, y) a1.plot(x, y) a2.plot(x, y)  f.tight_layout() 

enter image description here

like image 163
Hagne Avatar answered Sep 29 '22 11:09

Hagne


You can use gridspec and figure:

import numpy as np import matplotlib.pyplot as plt  from matplotlib import gridspec  # generate some data x = np.arange(0, 10, 0.2) y = np.sin(x)  # plot it fig = plt.figure(figsize=(8, 6))  gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])  ax0 = plt.subplot(gs[0]) ax0.plot(x, y) ax1 = plt.subplot(gs[1]) ax1.plot(y, x)  plt.tight_layout() plt.savefig('grid_figure.pdf') 

enter image description here

like image 32
bmu Avatar answered Sep 29 '22 10:09

bmu