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

Skip to content

Commit e097bf4

Browse files
timhoffmQuLogic
andauthored
Improve documentation on rasterization (#18969)
* Improve documentation on rasterization * Update examples/misc/rasterization_demo.py Co-authored-by: Elliott Sales de Andrade <[email protected]> * Update examples/misc/rasterization_demo.py Co-authored-by: Elliott Sales de Andrade <[email protected]> Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent 8244201 commit e097bf4

File tree

5 files changed

+78
-31
lines changed

5 files changed

+78
-31
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: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
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). It can
7+
speed up rendering and produce smaller files for large data sets, but comes
8+
at the cost of a fixed resolution.
89
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
10+
Whether rasterization should be used can be specified per artist. This can be
11+
useful to reduce the file size of large artists, while maintaining the
1212
advantages of vector graphics for other artists such as the axes
13-
and annotations. For instance a complicated `~.Axes.pcolormesh` or
13+
and text. For instance a complicated `~.Axes.pcolormesh` or
1414
`~.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`.
15+
Setting rasterization only affects vector backends such as PDF, SVG, or PS.
16+
17+
Rasterization is disabled by default. There are two ways to enable it, which
18+
can also be combined:
19+
20+
- Set `~.Artist.set_rasterized` on individual artists, or use the keyword
21+
argument *rasterized* when creating the artist.
22+
- Set `.Axes.set_rasterization_zorder` to rasterize all artists with a zorder
23+
less than the given value.
24+
25+
The storage size and the resolution of the rasterized artist is determined by
26+
its physical size and the value of the ``dpi`` parameter passed to
27+
`~.Figure.savefig`.
28+
29+
.. note::
30+
31+
The image of this example shown in the HTML documentation is not a vector
32+
graphic. Therefore, it cannot illustrate the rasterization effect. Please
33+
run this example locally and check the generated graphics files.
34+
1835
"""
1936

2037
import numpy as np
@@ -27,37 +44,35 @@
2744
xx = x*np.cos(theta) - y*np.sin(theta) # rotate x by -theta
2845
yy = x*np.sin(theta) + y*np.cos(theta) # rotate y by -theta
2946

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

33-
# Create a pseudocolor non-rastertized plot with a non-regular rectangular grid
49+
# pcolormesh without rasterization
3450
ax1.set_aspect(1)
3551
ax1.pcolormesh(xx, yy, d)
3652
ax1.set_title("No Rasterization")
3753

38-
# Create a pseudocolor rastertized plot with a non-regular rectangular grid
54+
# pcolormesh with rasterization; enabled by keyword argument
3955
ax2.set_aspect(1)
4056
ax2.set_title("Rasterization")
4157
m = ax2.pcolormesh(xx, yy, d, rasterized=True)
4258

43-
# Create a pseudocolor non-rastertized plot with a non-regular rectangular
44-
# grid and an overlapped "Text"
59+
# pcolormesh with an overlaid text without rasterization
4560
ax3.set_aspect(1)
4661
ax3.pcolormesh(xx, yy, d)
4762
ax3.text(0.5, 0.5, "Text", alpha=0.2,
4863
va="center", ha="center", size=50, transform=ax3.transAxes)
4964
ax3.set_title("No Rasterization")
5065

51-
# Create a pseudocolor rastertized plot with a non-regular rectangular
52-
# grid and an overlapped "Text"
66+
# pcolormesh with an overlaid text without rasterization; enabled by zorder.
67+
# Setting the rasterization zorder threshold to 0 and a negative zorder on the
68+
# pcolormesh rasterizes it. All artists have a non-negative zorder by default,
69+
# so they (e.g. the text here) are not affected.
5370
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,
71+
m = ax4.pcolormesh(xx, yy, d, zorder=-10)
72+
ax4.text(0.5, 0.5, "Text", alpha=0.2,
5673
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)
74+
ax4.set_rasterization_zorder(0)
5975
ax4.set_title("Rasterization z$<-10$")
60-
# ax2.title.set_rasterized(True) # should display a warning
6176

6277
# Save files in pdf and eps format
6378
plt.savefig("test_rasterization.pdf", dpi=150)
@@ -66,3 +81,19 @@
6681
if not plt.rcParams["text.usetex"]:
6782
plt.savefig("test_rasterization.svg", dpi=150)
6883
# svg backend currently ignores the dpi
84+
85+
#############################################################################
86+
#
87+
# ------------
88+
#
89+
# References
90+
# """"""""""
91+
#
92+
# The use of the following functions, methods, classes and modules is shown
93+
# in this example:
94+
95+
import matplotlib
96+
matplotlib.artist.Artist.set_rasterized
97+
matplotlib.axes.Axes.set_rasterization_zorder
98+
matplotlib.axes.Axes.pcolormesh
99+
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)