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

Skip to content

Commit b89eaf8

Browse files
committed
Improve documentation on rasterization
1 parent c486cd7 commit b89eaf8

File tree

5 files changed

+73
-28
lines changed

5 files changed

+73
-28
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ per-file-ignores =
180180
examples/misc/agg_buffer.py: E402
181181
examples/misc/histogram_path.py: E402
182182
examples/misc/print_stdout_sgskip.py: E402
183+
examples/misc/rasterization_demo.py: E402
183184
examples/misc/svg_filter_line.py: E402
184185
examples/misc/svg_filter_pie.py: E402
185186
examples/misc/table_demo.py: E201

examples/misc/rasterization_demo.py

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
"""
2-
==================
3-
Rasterization Demo
4-
==================
2+
=================================
3+
Rasterization for vector graphics
4+
=================================
55
6-
Rasterization is a method where an image described in a vector graphics
7-
format is being converted into a raster image (pixels).
6+
Rasterization converts vector graphics into a raster image (pixels).
87
98
Individual artists can be rasterized for saving to a vector backend
109
such as PDF, SVG, or PS as embedded images. This can be useful to
1110
reduce the file size of large artists, while maintaining the
1211
advantages of vector graphics for other artists such as the axes
13-
and annotations. For instance a complicated `~.Axes.pcolormesh` or
12+
and texts. For instance a complicated `~.Axes.pcolormesh` or
1413
`~.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`.
14+
15+
Rasterization is disabled by default. There are two ways to enable it, which
16+
can also be combined:
17+
18+
- Set `~.Artist.set_rasterized` on individual artists, or use the keyword
19+
argument *rasterized* when creating the artist.
20+
- Set `.Axes.set_rasterization_zorder` to rasterize all artists with a zorder
21+
less than the given value.
22+
23+
The storage size and the resolution of the rasterized artist is determined by
24+
its physical size and the value of the ``dpi`` parameter passed to
25+
`~.Figure.savefig`.
26+
27+
.. note::
28+
29+
The image of this example shown in the HTML documentation is not a vector
30+
graphics. Therefore, it cannot illustrate the rasterization effect. Please
31+
run this example locally and check the generated graphics files.
32+
1833
"""
1934

2035
import numpy as np
@@ -27,37 +42,35 @@
2742
xx = x*np.cos(theta) - y*np.sin(theta) # rotate x by -theta
2843
yy = x*np.sin(theta) + y*np.cos(theta) # rotate y by -theta
2944

30-
# Plot the rasterized and non-rasterized plot
3145
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, constrained_layout=True)
3246

33-
# Create a pseudocolor non-rastertized plot with a non-regular rectangular grid
47+
# pcolormesh without rasterization
3448
ax1.set_aspect(1)
3549
ax1.pcolormesh(xx, yy, d)
3650
ax1.set_title("No Rasterization")
3751

38-
# Create a pseudocolor rastertized plot with a non-regular rectangular grid
52+
# pcolormesh with rasterization; enabled by keyword argument
3953
ax2.set_aspect(1)
4054
ax2.set_title("Rasterization")
4155
m = ax2.pcolormesh(xx, yy, d, rasterized=True)
4256

43-
# Create a pseudocolor non-rastertized plot with a non-regular rectangular
44-
# grid and an overlapped "Text"
57+
# pcolormesh with an overlaid text without rasterization
4558
ax3.set_aspect(1)
4659
ax3.pcolormesh(xx, yy, d)
4760
ax3.text(0.5, 0.5, "Text", alpha=0.2,
4861
va="center", ha="center", size=50, transform=ax3.transAxes)
4962
ax3.set_title("No Rasterization")
5063

51-
# Create a pseudocolor rastertized plot with a non-regular rectangular
52-
# grid and an overlapped "Text"
64+
# pcolormesh with an overlaid text without rasterization; enabled by zorder.
65+
# Setting the rasterization zorder threshold to 0 and a negative zorder on the
66+
# pcolormesh rasterizes it. All artists have a non-negative zorder by default,
67+
# so they (e.g. the text here) are not affected.
5368
ax4.set_aspect(1)
54-
m = ax4.pcolormesh(xx, yy, d, zorder=-20)
55-
ax4.text(0.5, 0.5, "Text", alpha=0.2, zorder=-15,
69+
m = ax4.pcolormesh(xx, yy, d, zorder=-10)
70+
ax4.text(0.5, 0.5, "Text", alpha=0.2,
5671
va="center", ha="center", size=50, transform=ax4.transAxes)
57-
# Set zorder value below which artists will be rasterized
58-
ax4.set_rasterization_zorder(-10)
72+
ax4.set_rasterization_zorder(0)
5973
ax4.set_title("Rasterization z$<-10$")
60-
# ax2.title.set_rasterized(True) # should display a warning
6174

6275
# Save files in pdf and eps format
6376
plt.savefig("test_rasterization.pdf", dpi=150)
@@ -66,3 +79,19 @@
6679
if not plt.rcParams["text.usetex"]:
6780
plt.savefig("test_rasterization.svg", dpi=150)
6881
# svg backend currently ignores the dpi
82+
83+
#############################################################################
84+
#
85+
# ------------
86+
#
87+
# References
88+
# """"""""""
89+
#
90+
# The use of the following functions, methods, classes and modules is shown
91+
# in this example:
92+
93+
import matplotlib
94+
matplotlib.artist.Artist.set_rasterized
95+
matplotlib.axes.Axes.set_rasterization_zorder
96+
matplotlib.axes.Axes.pcolormesh
97+
matplotlib.pyplot.pcolormesh

lib/matplotlib/artist.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,15 @@ def get_rasterized(self):
886886

887887
def set_rasterized(self, rasterized):
888888
"""
889-
Force rasterized (bitmap) drawing in vector backend output.
889+
Force rasterized (bitmap) drawing for vector graphics output.
890890
891-
Defaults to None, which implies the backend's default behavior.
891+
Rasterized drawing is not supported by all artists. If you try to
892+
enable this on an artist that does not support it, the command has no
893+
effect and a warning will be issued.
894+
895+
This setting is ignored for pixel-based output.
896+
897+
See also :doc:`/gallery/misc/rasterization_demo`.
892898
893899
Parameters
894900
----------

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5834,7 +5834,6 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
58345834
differences see :ref:`Differences between pcolor() and pcolormesh()
58355835
<differences-pcolor-pcolormesh>`.
58365836
5837-
58385837
Parameters
58395838
----------
58405839
C : array-like
@@ -5927,8 +5926,9 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59275926
Whether to snap the mesh to pixel boundaries.
59285927
59295928
rasterized: bool, optional
5930-
Rasterize the pcolormesh before drawing. Can produce images
5931-
faster and make smaller files for large data sets.
5929+
Rasterize the pcolormesh when drawing vector graphics. This can
5930+
speed up rendering and produce smaller files for large data sets.
5931+
See also :doc:`/gallery/misc/rasterization_demo`.
59325932
59335933
Returns
59345934
-------

lib/matplotlib/axes/_base.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,11 +2488,20 @@ def margins(self, *margins, x=None, y=None, tight=True):
24882488

24892489
def set_rasterization_zorder(self, z):
24902490
"""
2491+
Set the zorder threshold for rasterization for vector graphics output.
2492+
2493+
All artists with a zorder below the given value will be rasterized if
2494+
they support rasterization.
2495+
2496+
This setting is ignored for pixel-based output.
2497+
2498+
See also :doc:`/gallery/misc/rasterization_demo`.
2499+
24912500
Parameters
24922501
----------
24932502
z : float or None
2494-
zorder below which artists are rasterized. ``None`` means that
2495-
artists do not get rasterized based on zorder.
2503+
The zorder below which artists are rasterized.
2504+
If ``None`` rasterization based on zorder is deactivated.
24962505
"""
24972506
self._rasterization_zorder = z
24982507
self.stale = True

0 commit comments

Comments
 (0)