|
3 | 3 | Rasterization Demo
|
4 | 4 | ==================
|
5 | 5 |
|
| 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`. |
6 | 18 | """
|
| 19 | + |
7 | 20 | import numpy as np
|
8 | 21 | import matplotlib.pyplot as plt
|
9 | 22 |
|
10 |
| -d = np.arange(100).reshape(10, 10) |
| 23 | +d = np.arange(100).reshape(10, 10) # the values to be color-mapped |
11 | 24 | x, y = np.meshgrid(np.arange(11), np.arange(11))
|
12 | 25 |
|
13 | 26 | 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 |
16 | 29 |
|
| 30 | +# Plot the rasterized and non-rasterized plot |
17 | 31 | fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
|
| 32 | + |
| 33 | +# Create a pseudocolor non-rastertized plot with a non-regular rectangular grid |
18 | 34 | ax1.set_aspect(1)
|
19 | 35 | ax1.pcolormesh(xx, yy, d)
|
20 | 36 | ax1.set_title("No Rasterization")
|
21 | 37 |
|
| 38 | +# Create a pseudocolor rastertized plot with a non-regular rectangular grid |
22 | 39 | ax2.set_aspect(1)
|
23 | 40 | ax2.set_title("Rasterization")
|
24 |
| - |
25 | 41 | m = ax2.pcolormesh(xx, yy, d)
|
| 42 | +# Force rasterized drawing in vector backend output |
26 | 43 | m.set_rasterized(True)
|
27 | 44 |
|
| 45 | +# Create a pseudocolor non-rastertized plot with a non-regular rectangular |
| 46 | +# grid and an overlapped "Text" |
28 | 47 | ax3.set_aspect(1)
|
29 | 48 | ax3.pcolormesh(xx, yy, d)
|
30 | 49 | ax3.text(0.5, 0.5, "Text", alpha=0.2,
|
31 | 50 | va="center", ha="center", size=50, transform=ax3.transAxes)
|
32 |
| - |
33 | 51 | ax3.set_title("No Rasterization")
|
34 | 52 |
|
35 |
| - |
| 53 | +# Create a pseudocolor rastertized plot with a non-regular rectangular |
| 54 | +# grid and an overlapped "Text" |
36 | 55 | ax4.set_aspect(1)
|
37 | 56 | m = ax4.pcolormesh(xx, yy, d)
|
38 | 57 | m.set_zorder(-20)
|
39 |
| - |
40 | 58 | ax4.text(0.5, 0.5, "Text", alpha=0.2,
|
41 | 59 | zorder=-15,
|
42 | 60 | va="center", ha="center", size=50, transform=ax4.transAxes)
|
43 |
| - |
| 61 | +# Set zorder value below which artists will be rasterized |
44 | 62 | ax4.set_rasterization_zorder(-10)
|
45 |
| - |
46 | 63 | ax4.set_title("Rasterization z$<-10$")
|
47 |
| - |
48 |
| - |
49 | 64 | # ax2.title.set_rasterized(True) # should display a warning
|
50 | 65 |
|
| 66 | +# Save files in pdf and eps format |
51 | 67 | plt.savefig("test_rasterization.pdf", dpi=150)
|
52 | 68 | plt.savefig("test_rasterization.eps", dpi=150)
|
53 | 69 |
|
|
0 commit comments