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

Skip to content

Commit a22d219

Browse files
committed
Improve documentation on rasterization
1 parent 02746fa commit a22d219

File tree

4 files changed

+77
-22
lines changed

4 files changed

+77
-22
lines changed

examples/misc/rasterization_demo.py

Lines changed: 46 additions & 18 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,34 @@
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+
# Even though the text is below the rasterization_zorder, it is not rasterized
66+
# because Text objects do not support rasterization.
5367
ax4.set_aspect(1)
5468
m = ax4.pcolormesh(xx, yy, d, zorder=-20)
5569
ax4.text(0.5, 0.5, "Text", alpha=0.2, zorder=-15,
5670
va="center", ha="center", size=50, transform=ax4.transAxes)
57-
# Set zorder value below which artists will be rasterized
5871
ax4.set_rasterization_zorder(-10)
5972
ax4.set_title("Rasterization z$<-10$")
60-
# ax2.title.set_rasterized(True) # should display a warning
6173

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

lib/matplotlib/artist.py

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

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

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

lib/matplotlib/tests/test_mathtext.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
from matplotlib import cbook, mathtext
1212

1313

14+
15+
def test_mathtext_cmr10_minus_sign():
16+
# cmr10 does not contain a minus sign and used to issue a warning
17+
# RuntimeWarning: Glyph 8722 missing from current font.
18+
mpl.rcParams['font.family'] = 'cmr10'
19+
fig, ax = plt.subplots()
20+
ax.plot(range(-1, 1), range(-1, 1))
21+
with pytest.warns(None) as record:
22+
fig.canvas.draw()
23+
assert len(record) == 0, (f"Got {len(record)} warnings:\n" + "\n".join(
24+
str(e.message) for e in record))
25+
1426
# If test is removed, use None as placeholder
1527
math_tests = [
1628
r'$a+b+\dot s+\dot{s}+\ldots$',

0 commit comments

Comments
 (0)