Rasterization helps make complex plots faster and smaller by turning detailed parts into images.
0
0
Rasterization for complex plots in Matplotlib
Introduction
When your plot has many points or lines and is slow to draw.
When saving plots as vector files that become very large.
When you want to keep text sharp but simplify complex shapes.
When sharing plots that need to load quickly on websites.
When combining detailed images with simple graphics in one plot.
Syntax
Matplotlib
plot_object.set_rasterized(True)You apply rasterization to parts of the plot like lines or collections.
This works well when saving to vector formats like PDF or SVG.
Examples
This example rasterizes the line plot to speed up saving as PDF.
Matplotlib
import matplotlib.pyplot as plt x = range(1000) y = [i**0.5 for i in x] plt.plot(x, y, rasterized=True) plt.savefig('plot.pdf')
Rasterizes only the scatter points in the SVG file.
Matplotlib
fig, ax = plt.subplots() scatter = ax.scatter(x, y) scatter.set_rasterized(True) plt.savefig('scatter.svg')
Sample Program
This code creates a noisy sine wave with many points and rasterizes the line to make saving faster and file smaller.
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 10000) y = np.sin(x) + np.random.normal(0, 0.1, x.size) fig, ax = plt.subplots() line, = ax.plot(x, y, label='Noisy sine wave') line.set_rasterized(True) ax.set_title('Rasterized Line Plot Example') ax.legend() plt.savefig('rasterized_plot.pdf') print('Plot saved as rasterized_plot.pdf')
OutputSuccess
Important Notes
Rasterization only affects vector output formats like PDF and SVG, not PNG or JPG.
Use rasterization on complex parts to keep text and labels sharp.
Too much rasterization can reduce quality, so use it wisely.
Summary
Rasterization turns complex plot parts into images to improve speed and file size.
Apply rasterization to lines, scatter points, or collections in matplotlib.
Best used when saving vector files with many details.