Introduction
In this lab, you will learn about rasterization for vector graphics. Rasterization is a process of converting vector graphics into a raster image (pixels). It can speed up rendering and produce smaller files for large data sets, but comes at the cost of a fixed resolution. We will be using Python Matplotlib library to illustrate the concept of rasterization.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import libraries
We need to import the required libraries before we start.
import matplotlib.pyplot as plt
import numpy as np
Create data
We will create some data which will be used to illustrate the rasterization concept.
d = np.arange(100).reshape(10, 10) ## the values to be color-mapped
x, y = np.meshgrid(np.arange(11), np.arange(11))
theta = 0.25*np.pi
xx = x*np.cos(theta) - y*np.sin(theta) ## rotate x by -theta
yy = x*np.sin(theta) + y*np.cos(theta) ## rotate y by -theta
Create a figure with four subplots
We will create a figure with four subplots to illustrate the different aspects of rasterization.
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, layout="constrained")
Create a pcolormesh plot without rasterization
We will create a pcolormesh plot without rasterization to illustrate the difference between rasterization and non-rasterization.
ax1.set_aspect(1)
ax1.pcolormesh(xx, yy, d)
ax1.set_title("No Rasterization")
Create a pcolormesh plot with rasterization
We will create a pcolormesh plot with rasterization to illustrate how rasterization can speed up rendering and produce smaller files.
ax2.set_aspect(1)
ax2.set_title("Rasterization")
ax2.pcolormesh(xx, yy, d, rasterized=True)
Create a pcolormesh plot with an overlaid text without rasterization
We will create a pcolormesh plot with an overlaid text without rasterization to illustrate how vector graphics can maintain the advantages of vector graphics for some artists such as the axes and text.
ax3.set_aspect(1)
ax3.pcolormesh(xx, yy, d)
ax3.text(0.5, 0.5, "Text", alpha=0.2,
va="center", ha="center", size=50, transform=ax3.transAxes)
ax3.set_title("No Rasterization")
Create a pcolormesh plot with an overlaid text with rasterization
We will create a pcolormesh plot with an overlaid text with rasterization to illustrate how rasterization can enable vector graphics to maintain the advantages of vector graphics for some artists such as the axes and text.
ax4.set_aspect(1)
m = ax4.pcolormesh(xx, yy, d, zorder=-10)
ax4.text(0.5, 0.5, "Text", alpha=0.2,
va="center", ha="center", size=50, transform=ax4.transAxes)
ax4.set_rasterization_zorder(0)
ax4.set_title("Rasterization z$<-10$")
Save the figures
We will save the figures in pdf and eps format.
plt.savefig("test_rasterization.pdf", dpi=150)
plt.savefig("test_rasterization.eps", dpi=150)
if not plt.rcParams["text.usetex"]:
plt.savefig("test_rasterization.svg", dpi=150)
## svg backend currently ignores the dpi
Summary
In this lab, we learned about rasterization for vector graphics. We used Python Matplotlib library to illustrate the concept of rasterization. We created a figure with four subplots to illustrate the different aspects of rasterization. We also learned how rasterization can speed up rendering and produce smaller files for large data sets, but comes at the cost of a fixed resolution. We also learned how rasterization can enable vector graphics to maintain the advantages of vector graphics for some artists such as the axes and text.