How to Create Matplotlib Plots Without a GUI
Last Updated :
05 Dec, 2024
To create and save plots using Matplotlib without opening a GUI window, you need to configure Matplotlib to use a non-interactive backend. This can be achieved by setting the backend to 'Agg', which is suitable for generating plots without displaying them. Let's see how to set the backend to Agg
:
Method 1: Using Non-Interactive Backends - (Agg )
Non-interactive backends like 'Agg' allow you to render plots without opening a GUI window. This is crucial for automation and running scripts on headless servers where GUI support is unavailable. To use a non-interactive backend, set it at the beginning of your script:
Python
import matplotlib
matplotlib.use('Agg') # Use the 'Agg' backend for non-GUI rendering
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('plot.png') # Save the plot as a PNG file
plt.close() # Close the plot to free up memory
Method 2: Saving Plots with savefig
The savefig
function is essential for saving plots directly to files without displaying them. It supports various formats like PNG, PDF, SVG, etc., making it versatile for different use cases.After plotting your data, use plt.savefig('filename.ext')
to save the plot:
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('plot.png')
Method 3: Set the backend via the matplotlibrc
configuration
You can also set the backend globally by modifying the matplotlib
configuration file (matplotlibrc
), or by passing the backend argument directly when importing: Locate or create the matplotlibrc
file. You can find the location using:
import matplotlib
print(matplotlib.matplotlib_fname())
Method 4: Turning Off Interactive Mode
Disabling interactive mode with plt.ioff()
prevents plots from being displayed automatically. This is useful when you want complete control over when and how plots are shown or saved. To turn off interactive mode:
Python
import matplotlib.pyplot as plt
# Turn off interactive mode
plt.ioff()
plt.plot([1, 2, 3], [4, 5, 6])
plt.title("Interactive Mode Disabled")
# The plot won't display automatically Save the plot instead
plt.savefig("plot.png")
If you don't want any GUI interaction and just want to save the plot directly to a file, you can use savefig()
without needing to call show()
. This is particularly useful in automated scripts.
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.savefig('plot.png')
Key Takeaways:
Agg
backend: Use this for a non-interactive, non-GUI backend.savefig()
: Save plots directly to files without needing to show them.
Using Agg
is ideal for automated or server-side plotting where you don't need to display the plot interactively.
Similar Reads
How to Create Subplots in Matplotlib with Python? Matplotlib is a widely used data visualization library in Python that provides powerful tools for creating a variety of plots. One of the most useful features of Matplotlib is its ability to create multiple subplots within a single figure using the plt.subplots() method. This allows users to display
6 min read
How to Create an Empty Figure with Matplotlib in Python? Creating a figure explicitly is an object-oriented style of interfacing with matplotlib. The figure is a basic building block of creating a plot as Matplotlib graphs our data on figures. This figure keeps track of all other components such as child axes, legends, title, axis, etc. Steps to create an
2 min read
How to Save a Plot to a File Using Matplotlib? Matplotlib is a popular Python library to create plots, charts, and graphs of different types. show() is used to plot a plot, but it doesn't save the plot to a file. In this article, we will see various methods through which a Matplotlib plot can be saved as an image file.Methods to Save a Plot in M
2 min read
How to Generate Subplots With Python's Matplotlib Data visualization plays a pivotal role in the process of analyzing and interpreting data. The Matplotlib library in Python offers a robust toolkit for crafting diverse plots and charts. One standout feature is its capability to generate subplots within a single figure, providing a valuable tool for
6 min read
How to Add Axes to a Figure in Matplotlib with Python? Matplotlib is a library in Python used to create figures and provide tools for customizing it. It allows plotting different types of data, geometrical figures. In this article, we will see how to add axes to a figure in matplotlib. We can add axes to a figure in matplotlib by passing a list argument
2 min read