Skip to content

Commit f341646

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 2ab32eb commit f341646

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

lib/matplotlib/tests/test_backend_svg.py

+89-1
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,94 @@ 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_tag(fig, tag):
137+
fd = BytesIO()
138+
fig.savefig(fd, format='svg')
139+
fd.seek(0)
140+
buf = fd.read().decode()
141+
fd.close()
142+
open("test.svg", "w").write(buf)
143+
return buf.count("<%s" % tag)
144+
145+
# No rasterized elements
146+
fig1 = plt.figure()
147+
ax1 = fig1.add_subplot(1, 1, 1)
148+
ax1.set_axis_off()
149+
for n in range(5):
150+
ax1.plot([0, 20], [0, n], "b-", rasterized=False)
151+
assert count_tag(fig1, "image") == 0
152+
assert count_tag(fig1, "path") == 6 # axis patch plus lines
153+
154+
# rasterized can be merged
155+
fig2 = plt.figure()
156+
ax2 = fig2.add_subplot(1, 1, 1)
157+
ax2.set_axis_off()
158+
for n in range(5):
159+
ax2.plot([0, 20], [0, n], "b-", rasterized=True)
160+
assert count_tag(fig2, "image") == 1
161+
assert count_tag(fig2, "path") == 1 # axis patch
162+
163+
# rasterized can't be merged without effecting draw order
164+
fig3 = plt.figure()
165+
ax3 = fig3.add_subplot(1, 1, 1)
166+
ax3.set_axis_off()
167+
for n in range(5):
168+
ax3.plot([0, 20], [n, 0], "b-", rasterized=False)
169+
ax3.plot([0, 20], [0, n], "b-", rasterized=True)
170+
assert count_tag(fig3, "image") == 5
171+
assert count_tag(fig3, "path") == 6
172+
173+
# rasterized whole axes
174+
fig4 = plt.figure()
175+
ax4 = fig4.add_subplot(1, 1, 1)
176+
ax4.set_axis_off()
177+
ax4.set_rasterized(True)
178+
for n in range(5):
179+
ax4.plot([0, 20], [n, 0], "b-", rasterized=False)
180+
ax4.plot([0, 20], [0, n], "b-", rasterized=True)
181+
assert count_tag(fig4, "image") == 1
182+
assert count_tag(fig4, "path") == 1
183+
184+
97185
@needs_usetex
98186
def test_missing_psfont(monkeypatch):
99187
"""An error is raised if a TeX font lacks a Type-1 equivalent"""

0 commit comments

Comments
 (0)