5 Best Ways to Set the Backend in Matplotlib in Python

Rate this post

πŸ’‘ Problem Formulation: When working with Matplotlib in Python, it’s often necessary to choose an appropriate backend depending on your work environment and needs. A backend in Matplotlib refers to the module it uses to produce plots. Changing the backend can be important for rendering plots in different formats (GUI windows, static images, or web application servers). This article demonstrates how to set Matplotlib’s backend in five different ways.

Method 1: Use the matplotlib.use() Function

One common approach to set the backend in Matplotlib is by using the matplotlib.use() function. This function allows you to specify the desired backend as a string. It’s important to call this function before importing matplotlib.pyplot to avoid any conflicts.

Here’s an example:

import matplotlib

matplotlib.use('TKAgg')  # Set the backend to 'TKAgg'
import matplotlib.pyplot as plt

# Continue with your plotting code
plt.plot([1, 2, 3], [4, 5, 6])

The output will be a window displaying a simple line plot created using the “TKAgg” backend.

This method is straight-forward and is typically done at the start of your script, ensuring that the desired backend is active before any plots are created. However, this step must be performed before importing matplotlib.pyplot which might be overlooked sometimes, causing potential issues.

Method 2: Set Backend via the MATPLOTLIBRC file

The MATPLOTLIBRC is a configuration file that Matplotlib searches for whenever it is imported. By defining your backend in this file, you can consistently use the same backend across all your projects without needing to call matplotlib.use() in every script.

Here’s an example:

# In your MATPLOTLIBRC file, add or update the following line:
backend : Agg

After updating the MATPLOTLIBRC file, every time you import Matplotlib, it will use the “Agg” backend to render plots as images without the need to specify it in code.

This persistent method is ideal for setting a default backend for all your projects. However, if you ever need to switch to a different backend for a specific project, you’ll need to alter the configuration file or override it within your script.

Method 3: Specifying Backend in an Environment Variable

Another way to define the backend for Matplotlib is by setting the MPLBACKEND environment variable. This can be set in your operating system’s environment or at the beginning of your Python script before importing Matplotlib.

Here’s an example:

import os

# Set the MPLBACKEND environment variable
os.environ['MPLBACKEND'] = 'module://my_backend'

import matplotlib.pyplot as plt

# Continue with your plotting code
plt.plot([7, 8, 9], [10, 11, 12])

The output is a plot rendered using the custom backend specified by 'module://my_backend'.

This method is convenient for temporary changes or for deploying scripts on different systems with varying requirements. However, it could introduce complexity when managing environments and dependencies.

Method 4: Use IPython Magic Command (for IPython and Jupyter users)

If you’re using an IPython or Jupyter environment, you can use the magic command %matplotlib to set the backend inline with your notebook or console session.

Here’s an example:

%matplotlib inline
import matplotlib.pyplot as plt

# Continue with your plotting code
plt.plot([13, 14, 15], [16, 17, 18])

The output is a static image of the plot embedded directly inline with the notebook or IPython console.

This method is very user-friendly and simplifies backend management for interactive environments. However, it’s limited to IPython and Jupyter and isn’t suitable for script files or other Python environments.

Bonus One-Liner Method 5: Command-Line Argument

You can also set the backend when running a script from the command line by passing the --matplotlib flag followed by the backend name. This is specific to IPython.

Here’s an example:

ipython --matplotlib=qt5 myscript.py

The qt5 backend will be used when running myscript.py without the need to modify the script itself to set the backend.

This approach is very simple and perfect for testing different backends quickly. However, it only works with IPython and isn’t applicable in standard Python script execution.


  • Method 1: Use matplotlib.use() Function. Very explicit and script-based. Requires to be set before importing pyplot.
  • Method 2: Set Backend via MATPLOTLIBRC file. Provides a consistent default backend. Not convenient for project-specific backend changes.
  • Method 3: Specify Backend in an Environment Variable. Flexible and system-wide. Can complicate environment management and script deployment.
  • Method 4: Use IPython Magic Command. Ideal for interactive sessions. Limited to the IPython/Jupyter environment.
  • Method 5: Command-Line Argument. Quick for backend testing on IPython. Not suitable for regular Python scripts.