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

Skip to content

Commit 9344cb4

Browse files
authored
Merge pull request #15900 from anntzer/test_cycles
Rewrite test_cycles to avoid image comparison tests.
2 parents 7824648 + 6a1a07e commit 9344cb4

8 files changed

+51
-90
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

lib/matplotlib/tests/test_cycles.py

Lines changed: 51 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,101 @@
1-
import platform
2-
3-
from matplotlib.testing.decorators import image_comparison
1+
import matplotlib as mpl
42
import matplotlib.pyplot as plt
53
import numpy as np
64
import pytest
75

86
from cycler import cycler
97

108

11-
@image_comparison(['color_cycle_basic.png'], remove_text=True,
12-
tol={'aarch64': 0.02}.get(platform.machine(), 0.0))
139
def test_colorcycle_basic():
1410
fig, ax = plt.subplots()
1511
ax.set_prop_cycle(cycler('color', ['r', 'g', 'y']))
16-
xs = np.arange(10)
17-
ys = 0.25 * xs + 2
18-
ax.plot(xs, ys, label='red', lw=4)
19-
ys = 0.45 * xs + 3
20-
ax.plot(xs, ys, label='green', lw=4)
21-
ys = 0.65 * xs + 4
22-
ax.plot(xs, ys, label='yellow', lw=4)
23-
ys = 0.85 * xs + 5
24-
ax.plot(xs, ys, label='red2', lw=4)
25-
ax.legend(loc='upper left')
26-
27-
28-
@image_comparison(['marker_cycle.png', 'marker_cycle.png'], remove_text=True,
29-
tol={'aarch64': 0.02}.get(platform.machine(), 0.0))
12+
for _ in range(4):
13+
ax.plot(range(10), range(10))
14+
assert [l.get_color() for l in ax.lines] == ['r', 'g', 'y', 'r']
15+
16+
3017
def test_marker_cycle():
3118
fig, ax = plt.subplots()
3219
ax.set_prop_cycle(cycler('c', ['r', 'g', 'y']) +
3320
cycler('marker', ['.', '*', 'x']))
34-
xs = np.arange(10)
35-
ys = 0.25 * xs + 2
36-
ax.plot(xs, ys, label='red dot', lw=4, ms=16)
37-
ys = 0.45 * xs + 3
38-
ax.plot(xs, ys, label='green star', lw=4, ms=16)
39-
ys = 0.65 * xs + 4
40-
ax.plot(xs, ys, label='yellow x', lw=4, ms=16)
41-
ys = 0.85 * xs + 5
42-
ax.plot(xs, ys, label='red2 dot', lw=4, ms=16)
43-
ax.legend(loc='upper left')
21+
for _ in range(4):
22+
ax.plot(range(10), range(10))
23+
assert [l.get_color() for l in ax.lines] == ['r', 'g', 'y', 'r']
24+
assert [l.get_marker() for l in ax.lines] == ['.', '*', 'x', '.']
4425

26+
27+
def test_marker_cycle_kwargs_arrays_iterators():
4528
fig, ax = plt.subplots()
46-
# Test keyword arguments, numpy arrays, and generic iterators
4729
ax.set_prop_cycle(c=np.array(['r', 'g', 'y']),
4830
marker=iter(['.', '*', 'x']))
49-
xs = np.arange(10)
50-
ys = 0.25 * xs + 2
51-
ax.plot(xs, ys, label='red dot', lw=4, ms=16)
52-
ys = 0.45 * xs + 3
53-
ax.plot(xs, ys, label='green star', lw=4, ms=16)
54-
ys = 0.65 * xs + 4
55-
ax.plot(xs, ys, label='yellow x', lw=4, ms=16)
56-
ys = 0.85 * xs + 5
57-
ax.plot(xs, ys, label='red2 dot', lw=4, ms=16)
58-
ax.legend(loc='upper left')
59-
60-
61-
@image_comparison(['lineprop_cycle_basic.png'], remove_text=True,
62-
tol={'aarch64': 0.02}.get(platform.machine(), 0.0))
31+
for _ in range(4):
32+
ax.plot(range(10), range(10))
33+
assert [l.get_color() for l in ax.lines] == ['r', 'g', 'y', 'r']
34+
assert [l.get_marker() for l in ax.lines] == ['.', '*', 'x', '.']
35+
36+
6337
def test_linestylecycle_basic():
6438
fig, ax = plt.subplots()
6539
ax.set_prop_cycle(cycler('ls', ['-', '--', ':']))
66-
xs = np.arange(10)
67-
ys = 0.25 * xs + 2
68-
ax.plot(xs, ys, label='solid', lw=4, color='k')
69-
ys = 0.45 * xs + 3
70-
ax.plot(xs, ys, label='dashed', lw=4, color='k')
71-
ys = 0.65 * xs + 4
72-
ax.plot(xs, ys, label='dotted', lw=4, color='k')
73-
ys = 0.85 * xs + 5
74-
ax.plot(xs, ys, label='solid2', lw=4, color='k')
75-
ax.legend(loc='upper left')
76-
77-
78-
@image_comparison(['fill_cycle_basic.png'], remove_text=True)
40+
for _ in range(4):
41+
ax.plot(range(10), range(10))
42+
assert [l.get_linestyle() for l in ax.lines] == ['-', '--', ':', '-']
43+
44+
7945
def test_fillcycle_basic():
8046
fig, ax = plt.subplots()
8147
ax.set_prop_cycle(cycler('c', ['r', 'g', 'y']) +
8248
cycler('hatch', ['xx', 'O', '|-']) +
8349
cycler('linestyle', ['-', '--', ':']))
84-
xs = np.arange(10)
85-
ys = 0.25 * xs**.5 + 2
86-
ax.fill(xs, ys, label='red, xx', linewidth=3)
87-
ys = 0.45 * xs**.5 + 3
88-
ax.fill(xs, ys, label='green, circle', linewidth=3)
89-
ys = 0.65 * xs**.5 + 4
90-
ax.fill(xs, ys, label='yellow, cross', linewidth=3)
91-
ys = 0.85 * xs**.5 + 5
92-
ax.fill(xs, ys, label='red2, xx', linewidth=3)
93-
ax.legend(loc='upper left')
94-
95-
96-
@image_comparison(['fill_cycle_ignore.png'], remove_text=True)
50+
for _ in range(4):
51+
ax.fill(range(10), range(10))
52+
assert ([p.get_facecolor() for p in ax.patches]
53+
== [mpl.colors.to_rgba(c) for c in ['r', 'g', 'y', 'r']])
54+
assert [p.get_hatch() for p in ax.patches] == ['xx', 'O', '|-', 'xx']
55+
assert [p.get_linestyle() for p in ax.patches] == ['-', '--', ':', '-']
56+
57+
9758
def test_fillcycle_ignore():
9859
fig, ax = plt.subplots()
9960
ax.set_prop_cycle(cycler('color', ['r', 'g', 'y']) +
10061
cycler('hatch', ['xx', 'O', '|-']) +
10162
cycler('marker', ['.', '*', 'D']))
102-
xs = np.arange(10)
103-
ys = 0.25 * xs**.5 + 2
63+
t = range(10)
10464
# Should not advance the cycler, even though there is an
10565
# unspecified property in the cycler "marker".
10666
# "marker" is not a Polygon property, and should be ignored.
107-
ax.fill(xs, ys, 'r', hatch='xx', label='red, xx')
108-
ys = 0.45 * xs**.5 + 3
67+
ax.fill(t, t, 'r', hatch='xx')
10968
# Allow the cycler to advance, but specify some properties
110-
ax.fill(xs, ys, hatch='O', label='red, circle')
111-
ys = 0.65 * xs**.5 + 4
112-
ax.fill(xs, ys, label='green, circle')
113-
ys = 0.85 * xs**.5 + 5
114-
ax.fill(xs, ys, label='yellow, cross')
115-
ax.legend(loc='upper left')
69+
ax.fill(t, t, hatch='O')
70+
ax.fill(t, t)
71+
ax.fill(t, t)
72+
assert ([p.get_facecolor() for p in ax.patches]
73+
== [mpl.colors.to_rgba(c) for c in ['r', 'r', 'g', 'y']])
74+
assert [p.get_hatch() for p in ax.patches] == ['xx', 'O', 'O', '|-']
11675

11776

118-
@image_comparison(['property_collision_plot.png'], remove_text=True)
11977
def test_property_collision_plot():
12078
fig, ax = plt.subplots()
12179
ax.set_prop_cycle('linewidth', [2, 4])
80+
t = range(10)
12281
for c in range(1, 4):
123-
ax.plot(np.arange(10), c * np.arange(10), lw=0.1, color='k')
124-
ax.plot(np.arange(10), 4 * np.arange(10), color='k')
125-
ax.plot(np.arange(10), 5 * np.arange(10), color='k')
82+
ax.plot(t, t, lw=0.1)
83+
ax.plot(t, t)
84+
ax.plot(t, t)
85+
assert [l.get_linewidth() for l in ax.lines] == [0.1, 0.1, 0.1, 2, 4]
12686

12787

128-
@image_comparison(['property_collision_fill.png'], remove_text=True)
12988
def test_property_collision_fill():
13089
fig, ax = plt.subplots()
131-
xs = np.arange(10)
132-
ys = 0.25 * xs**.5 + 2
13390
ax.set_prop_cycle(linewidth=[2, 3, 4, 5, 6], facecolor='bgcmy')
91+
t = range(10)
13492
for c in range(1, 4):
135-
ax.fill(xs, c * ys, lw=0.1)
136-
ax.fill(xs, 4 * ys)
137-
ax.fill(xs, 5 * ys)
93+
ax.fill(t, t, lw=0.1)
94+
ax.fill(t, t)
95+
ax.fill(t, t)
96+
assert ([p.get_facecolor() for p in ax.patches]
97+
== [mpl.colors.to_rgba(c) for c in 'bgcmy'])
98+
assert [p.get_linewidth() for p in ax.patches] == [0.1, 0.1, 0.1, 5, 6]
13899

139100

140101
def test_valid_input_forms():

0 commit comments

Comments
 (0)