Skip to content

Commit e85b6be

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 56c6d5d commit e85b6be

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

lib/matplotlib/tests/test_backend_svg.py

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

0 commit comments

Comments
 (0)