5 Best Ways to Set the Matplotlib Figure Default Size in IPython Notebook

Rate this post

πŸ’‘ Problem Formulation: When working with Matplotlib in an IPython notebook, it’s common to need consistent figure sizes for better readability and professional presentation. Users often want to set a default figure size that applies to all plots without specifying dimensions for each plot individually. For instance, a user might want the default size to be 10×6 inches for all figures.

Method 1: Configure Inline Backend with Matplotlib RC Params

This method involves setting default parameters through Matplotlib’s ‘rcParams’ configuration dictionary. It affects figures globally within the notebook environment, making it consistent across all plots. By defining the ‘figure.figsize’ key, you can set a default figure size in inches.

Here’s an example:

import matplotlib.pyplot as plt

# Set default figure size
plt.rcParams['figure.figsize'] = [10, 6]

Output: All subsequent figures created will default to 10×6 inches.

By setting plt.rcParams['figure.figsize'] to a list of two values, you assign the default width and height (in inches) for figures in the entire notebook. This approach is straightforward and can be easily adjusted within a cell at any point.

Method 2: Adjust Display Settings within IPython Magic

The IPython environment allows users to control the backend of Matplotlib with magic functions. Using the ‘%matplotlib inline’ magic function with its backend configuration parameters, you can easily set the default figure size for your notebook.

Here’s an example:

%matplotlib inline
%config InlineBackend.figure_format = 'retina'
%config InlineBackend.rc = {'figure.figsize': (10, 6)}

Output: Figures will render inline at a size of 10×6 inches and in ‘retina’ quality, where supported.

This snippet combines the use of IPython magic commands and backend configuration to control figure aesthetics. It’s an IPython-specific method which is not portable to other Python environments but offers convenience when working within notebooks.

Method 3: Use the ‘pyplot’ Function for Individual Figures

If you prefer setting the figure size on a case-by-case basis, you can use the pyplot.figure() function. This gives you the flexibility to vary sizes for different plots, overriding the global configuration.

Here’s an example:

import matplotlib.pyplot as plt

# Create a figure with specific size
fig = plt.figure(figsize=(10, 6))

Output: A new figure with dimensions 10×6 inches.

This method gives precise control over the size of an individual figure by passing the figsize argument to plt.figure(). However, you’ll need to specify this every time you create a new figure, which can be repetitive.

Method 4: Define a Function to Set Default Size

For repeated adjustments within the same session, writing a custom function to set the figure size can save time. Call this function whenever you want to reset the figure size.

Here’s an example:

import matplotlib.pyplot as plt

def set_figsize(width=10, height=6):
    plt.rcParams['figure.figsize'] = [width, height]

# Set default figure size using the function

Output: All subsequent figures will now default to the set size within the function.

This custom function approach abstracts away the direct manipulation of plt.rcParams, making it more readable and reusable throughout your notebook. It’s a practical way to encapsulate configuration logic, but like method 1, it changes the settings globally.

Bonus One-Liner Method 5: Direct Inline Magic Command

For a quick one-liner approach, you can use the ‘%matplotlib inline’ magic command followed by the figure size parameters directly, without additional backend configuration steps.

Here’s an example:

%matplotlib inline --figsize=10,6

Output: Sets the default figure size to 10×6 inches for all figures rendered inline.

This method is short and to the point, ideal for quick adjustments without diving into backend settings. It is, however, less known and might not offer the configurability or clarity of the other methods.


  • Method 1: Configuration Via RC Params. Strengths: Easy to use, global setting. Weaknesses: Changes the default for all figures.
  • Method 2: IPython Magic Backend Settings. Strengths: Inline control, improved display options like ‘retina’. Weaknesses: Specific to IPython, non-portable outside notebooks.
  • Method 3: Pyplot Individual Figure Setting. Strengths: Per-figure control, flexibility. Weaknesses: Repetitive for multiple figures.
  • Method 4: Custom Function for Size Setting. Strengths: Reusability, improved code organization. Weaknesses: Still a global setting requiring function calls.
  • Method 5: Direct Inline Magic Command. Strengths: Quick and easy one-liner. Weaknesses: Limited control and customization, lesser-known approach.