How to change size of Matplotlib plot

Sometimes you may want to change the width or height or both of the plot figure generated by Matplotlib. For example, you may want the X-axis to be stretched out a bit more and so on.

The size of the plot can be changed by setting the dynamic rc settings of Matplotlib. These are stored in a dictionary named rcParams. The size of the plot figure is stored with the key figure.figsize.

As an example, to get and set the size of a Matplotlib plot:

import matplotlib.pyplot as plt

# Get current size
fig_size = plt.rcParams["figure.figsize"]

# Prints: [8.0, 6.0]
print "Current size:", fig_size

# Set figure width to 12 and height to 9
fig_size[0] = 12
fig_size[1] = 9
plt.rcParams["figure.figsize"] = fig_size

Tried with: Python 2.7.6 and Ubuntu 14.04