Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Matplotlib Backend Differences between Agg and Cairo
Matplotlib offers different backends for rendering graphics, each optimized for specific output formats. The Agg and Cairo backends are two popular choices with distinct capabilities and use cases.
Backend Comparison
| Backend | File Types | Graphics Type | Description |
|---|---|---|---|
| Agg | PNG | Raster | High-quality images using Anti-Grain Geometry engine |
| Cairo | PNG, PS, PDF, SVG | Raster & Vector | Versatile output using Cairo graphics library |
Using Agg Backend
The Agg backend is ideal for high-quality raster images. Here's how to use it ?
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
# Configure figure settings
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Set backend to Agg
mpl.use("Agg")
# Create sample data
data = np.random.rand(5, 5)
# Create and display the plot
plt.imshow(data, interpolation='nearest', cmap="copper")
plt.colorbar()
plt.title("Agg Backend Example")
# Save the figure
plt.savefig('agg_output.png', dpi=150)
print("Image saved as agg_output.png")
Image saved as agg_output.png
Using Cairo Backend
Cairo backend supports multiple output formats including vector graphics ?
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
# Set backend to Cairo
mpl.use("Cairo")
# Create sample data
data = np.random.rand(5, 5)
# Create the plot
plt.figure(figsize=(7.5, 3.5))
plt.imshow(data, interpolation='nearest', cmap="viridis")
plt.colorbar()
plt.title("Cairo Backend Example")
# Save in different formats
plt.savefig('cairo_output.png') # Raster
plt.savefig('cairo_output.pdf') # Vector
plt.savefig('cairo_output.svg') # Vector
Key Differences
Performance
Agg backend typically offers faster rendering for raster graphics, while Cairo provides more flexibility at the cost of slightly slower performance.
Output Quality
Both backends produce high-quality output, but Cairo's vector formats (PDF, SVG) remain crisp at any zoom level.
Use Cases
- Agg: Web graphics, data visualization dashboards, PNG exports
- Cairo: Scientific publications, scalable graphics, multi-format output
Switching Backends
import matplotlib as mpl
# Check current backend
print(f"Current backend: {mpl.get_backend()}")
# Switch to Agg
mpl.use("Agg")
print(f"New backend: {mpl.get_backend()}")
# List available backends
print("Available backends:", mpl.backend_bases.Backend.__subclasses__())
Current backend: module://matplotlib_inline.backend_inline New backend: Agg Available backends: [<class 'matplotlib.backends.backend_agg.RendererAgg'>, ...]
Conclusion
Choose Agg backend for fast, high-quality PNG output in web applications. Use Cairo backend when you need multiple output formats or scalable vector graphics for publications.
