Skip to content

Commit 9e80d62

Browse files
committed
Rasterization tests
Test that rasterization does not significantly change output. Tests that partial rasterization does not effect draw ordering. Tests that right number of <image> elements appear in SVG output, i.e. bitmaps are merged when appropriate.
1 parent 3ae9d44 commit 9e80d62

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from matplotlib import dviread
1111
from matplotlib.figure import Figure
1212
import matplotlib.pyplot as plt
13-
from matplotlib.testing.decorators import image_comparison
13+
from matplotlib.testing.decorators import image_comparison, check_figures_equal
1414

1515

1616
needs_usetex = pytest.mark.skipif(
@@ -94,6 +94,76 @@ def test_bold_font_output_with_none_fonttype():
9494
ax.set_title('bold-title', fontweight='bold')
9595

9696

97+
@check_figures_equal(tol=20)
98+
def test_rasterized(fig_test, fig_ref):
99+
t = np.arange(0, 100) * (2.3)
100+
x = np.cos(t)
101+
y = np.sin(t)
102+
103+
ax_ref = fig_ref.subplots()
104+
ax_ref.plot(x, y, "-", c="r", lw=10)
105+
ax_ref.plot(x+1, y, "-", c="b", lw=10)
106+
107+
ax_test = fig_test.subplots()
108+
ax_test.plot(x, y, "-", c="r", lw=10, rasterized=True)
109+
ax_test.plot(x+1, y, "-", c="b", lw=10, rasterized=True)
110+
111+
112+
@check_figures_equal()
113+
def test_rasterized_ordering(fig_test, fig_ref):
114+
t = np.arange(0, 100) * (2.3)
115+
x = np.cos(t)
116+
y = np.sin(t)
117+
118+
ax_ref = fig_ref.subplots()
119+
ax_ref.set_xlim(0, 3)
120+
ax_ref.set_ylim(-1.1, 1.1)
121+
ax_ref.plot(x, y, "-", c="r", lw=10, rasterized=True)
122+
ax_ref.plot(x+1, y, "-", c="b", lw=10, rasterized=False)
123+
ax_ref.plot(x+2, y, "-", c="g", lw=10, rasterized=True)
124+
ax_ref.plot(x+3, y, "-", c="m", lw=10, rasterized=True)
125+
126+
ax_test = fig_test.subplots()
127+
ax_test.set_xlim(0, 3)
128+
ax_test.set_ylim(-1.1, 1.1)
129+
ax_test.plot(x, y, "-", c="r", lw=10, rasterized=True, zorder=1.1)
130+
ax_test.plot(x+2, y, "-", c="g", lw=10, rasterized=True, zorder=1.3)
131+
ax_test.plot(x+3, y, "-", c="m", lw=10, rasterized=True, zorder=1.4)
132+
ax_test.plot(x+1, y, "-", c="b", lw=10, rasterized=False, zorder=1.2)
133+
134+
135+
def test_count_bitmaps():
136+
def count_images(fig):
137+
fd = BytesIO()
138+
fig.savefig(fd, format='svg')
139+
fd.seek(0)
140+
buf = fd.read().decode()
141+
fd.close()
142+
return buf.count("<image")
143+
144+
# No rasterized elements
145+
fig1 = plt.figure()
146+
ax1 = fig1.add_subplot(1, 1, 1)
147+
for n in range(5):
148+
ax1.plot([0, 20], [0, n], "b-", rasterized=False)
149+
assert count_images(fig1) == 0
150+
151+
# rasterized can be merged
152+
fig2 = plt.figure()
153+
ax2 = fig2.add_subplot(1, 1, 1)
154+
for n in range(5):
155+
ax2.plot([0, 20], [0, n], "b-", rasterized=True)
156+
assert count_images(fig2) == 1
157+
158+
# rasterized can't be merged without effecting draw order
159+
fig3 = plt.figure()
160+
ax3 = fig3.add_subplot(1, 1, 1)
161+
for n in range(5):
162+
ax3.plot([0, 20], [n, 0], "b-", rasterized=False)
163+
ax3.plot([0, 20], [0, n], "b-", rasterized=True)
164+
assert count_images(fig3) == 5
165+
166+
97167
@needs_usetex
98168
def test_missing_psfont(monkeypatch):
99169
"""An error is raised if a TeX font lacks a Type-1 equivalent"""

0 commit comments

Comments
 (0)