Python
matplotlib
PNG
DISPLAY variable
headless rendering

Generating a PNG with matplotlib when DISPLAY is undefined

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

When working with `matplotlib` in environments where a graphical user interface is unavailable, such as remote servers or certain automated testing systems, you might encounter a scenario where the `DISPLAY` variable is undefined. In Unix-like systems, `DISPLAY` is an environment variable used by X11 to identify the display server for rendering graphical output. When it's unavailable, you need an alternative approach to generate visual content, such as PNG images, for your plots. Fortunately, `matplotlib` provides a non-interactive backend that can handle such situations. This article explores the mechanisms to generate PNG images using `matplotlib` when `DISPLAY` is undefined, providing technical explanations and relevant examples.

Understanding Matplotlib Backends

`matplotlib` is a versatile library that uses different backends to render plots. Backends can be broadly categorized into two types:

  • Interactive Backends: These backends (e.g., `QtAgg`, `TkAgg`, `GTK3Agg`) support real-time plot updates and user interactions. They require a graphical display environment.
  • Non-Interactive Backends: These backends (e.g., `Agg`, `PDF`, `SVG`) are designed for environments without a graphical interface, generating static file outputs instead.

Using the Agg Backend

The `Agg` backend is ideal for non-interactive use cases, specifically when you need to create PNG files without a display server. It renders the plot to an off-screen buffer and exports it directly to an image file. Here are the steps to achieve this:

  1. Set the Backend: Configure `matplotlib` to use the `Agg` backend before any plot operations. This can be done programmatically or via configuration files.
  • Install Xvfb: First, ensure that Xvfb is installed on your system.
  • Run Xvfb: You can start a virtual display with a specific display number.
  • Environment Configuration: Ensure proper setup of Python environments and necessary packages when running headless `matplotlib` scripts on servers or Docker containers.
  • Performance: While the `Agg` backend is efficient for PNG generation, it's essential to monitor memory and CPU usage, especially when generating a large number of plots concurrently.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.