Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e54cf8f

Browse files
authored
Add explanatory text for rasterization demo (#18012)
* Add explanatory text for rasterization demo * Add purpose of demo, edit consistency of python code * Edit python code based on flake8 comments * Edit comments about the purpose of demo * Edit comments * Removed trailing whitespaces
1 parent 083a193 commit e54cf8f

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

examples/misc/rasterization_demo.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,67 @@
33
Rasterization Demo
44
==================
55
6+
Rasterization is a method where an image described in a vector graphics
7+
format is being converted into a raster image (pixels).
8+
9+
Individual artists can be rasterized for saving to a vector backend
10+
such as PDF, SVG, or PS as embedded images. This can be useful to
11+
reduce the file size of large artists, while maintaining the
12+
advantages of vector graphics for other artists such as the axes
13+
and annotations. For instance a complicated `~.Axes.pcolormesh` or
14+
`~.Axes.contourf` can be made significantly simpler by rasterizing.
15+
Note that the size and resolution of the rasterized artist is
16+
controlled by its physical size and the value of the ``dpi`` kwarg
17+
passed to `~.Figure.savefig`.
618
"""
19+
720
import numpy as np
821
import matplotlib.pyplot as plt
922

10-
d = np.arange(100).reshape(10, 10)
23+
d = np.arange(100).reshape(10, 10) # the values to be color-mapped
1124
x, y = np.meshgrid(np.arange(11), np.arange(11))
1225

1326
theta = 0.25*np.pi
14-
xx = x*np.cos(theta) - y*np.sin(theta)
15-
yy = x*np.sin(theta) + y*np.cos(theta)
27+
xx = x*np.cos(theta) - y*np.sin(theta) # rotate x by -theta
28+
yy = x*np.sin(theta) + y*np.cos(theta) # rotate y by -theta
1629

30+
# Plot the rasterized and non-rasterized plot
1731
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
32+
33+
# Create a pseudocolor non-rastertized plot with a non-regular rectangular grid
1834
ax1.set_aspect(1)
1935
ax1.pcolormesh(xx, yy, d)
2036
ax1.set_title("No Rasterization")
2137

38+
# Create a pseudocolor rastertized plot with a non-regular rectangular grid
2239
ax2.set_aspect(1)
2340
ax2.set_title("Rasterization")
24-
2541
m = ax2.pcolormesh(xx, yy, d)
42+
# Force rasterized drawing in vector backend output
2643
m.set_rasterized(True)
2744

45+
# Create a pseudocolor non-rastertized plot with a non-regular rectangular
46+
# grid and an overlapped "Text"
2847
ax3.set_aspect(1)
2948
ax3.pcolormesh(xx, yy, d)
3049
ax3.text(0.5, 0.5, "Text", alpha=0.2,
3150
va="center", ha="center", size=50, transform=ax3.transAxes)
32-
3351
ax3.set_title("No Rasterization")
3452

35-
53+
# Create a pseudocolor rastertized plot with a non-regular rectangular
54+
# grid and an overlapped "Text"
3655
ax4.set_aspect(1)
3756
m = ax4.pcolormesh(xx, yy, d)
3857
m.set_zorder(-20)
39-
4058
ax4.text(0.5, 0.5, "Text", alpha=0.2,
4159
zorder=-15,
4260
va="center", ha="center", size=50, transform=ax4.transAxes)
43-
61+
# Set zorder value below which artists will be rasterized
4462
ax4.set_rasterization_zorder(-10)
45-
4663
ax4.set_title("Rasterization z$<-10$")
47-
48-
4964
# ax2.title.set_rasterized(True) # should display a warning
5065

66+
# Save files in pdf and eps format
5167
plt.savefig("test_rasterization.pdf", dpi=150)
5268
plt.savefig("test_rasterization.eps", dpi=150)
5369

0 commit comments

Comments
 (0)