|
1 | | -import platform |
2 | | - |
3 | | -from matplotlib.testing.decorators import image_comparison |
| 1 | +import matplotlib as mpl |
4 | 2 | import matplotlib.pyplot as plt |
5 | 3 | import numpy as np |
6 | 4 | import pytest |
7 | 5 |
|
8 | 6 | from cycler import cycler |
9 | 7 |
|
10 | 8 |
|
11 | | -@image_comparison(['color_cycle_basic.png'], remove_text=True, |
12 | | - tol={'aarch64': 0.02}.get(platform.machine(), 0.0)) |
13 | 9 | def test_colorcycle_basic(): |
14 | 10 | fig, ax = plt.subplots() |
15 | 11 | 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 | + |
30 | 17 | def test_marker_cycle(): |
31 | 18 | fig, ax = plt.subplots() |
32 | 19 | ax.set_prop_cycle(cycler('c', ['r', 'g', 'y']) + |
33 | 20 | 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', '.'] |
44 | 25 |
|
| 26 | + |
| 27 | +def test_marker_cycle_kwargs_arrays_iterators(): |
45 | 28 | fig, ax = plt.subplots() |
46 | | - # Test keyword arguments, numpy arrays, and generic iterators |
47 | 29 | ax.set_prop_cycle(c=np.array(['r', 'g', 'y']), |
48 | 30 | 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 | + |
63 | 37 | def test_linestylecycle_basic(): |
64 | 38 | fig, ax = plt.subplots() |
65 | 39 | 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 | + |
79 | 45 | def test_fillcycle_basic(): |
80 | 46 | fig, ax = plt.subplots() |
81 | 47 | ax.set_prop_cycle(cycler('c', ['r', 'g', 'y']) + |
82 | 48 | cycler('hatch', ['xx', 'O', '|-']) + |
83 | 49 | 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 | + |
97 | 58 | def test_fillcycle_ignore(): |
98 | 59 | fig, ax = plt.subplots() |
99 | 60 | ax.set_prop_cycle(cycler('color', ['r', 'g', 'y']) + |
100 | 61 | cycler('hatch', ['xx', 'O', '|-']) + |
101 | 62 | cycler('marker', ['.', '*', 'D'])) |
102 | | - xs = np.arange(10) |
103 | | - ys = 0.25 * xs**.5 + 2 |
| 63 | + t = range(10) |
104 | 64 | # Should not advance the cycler, even though there is an |
105 | 65 | # unspecified property in the cycler "marker". |
106 | 66 | # "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') |
109 | 68 | # 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', '|-'] |
116 | 75 |
|
117 | 76 |
|
118 | | -@image_comparison(['property_collision_plot.png'], remove_text=True) |
119 | 77 | def test_property_collision_plot(): |
120 | 78 | fig, ax = plt.subplots() |
121 | 79 | ax.set_prop_cycle('linewidth', [2, 4]) |
| 80 | + t = range(10) |
122 | 81 | 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] |
126 | 86 |
|
127 | 87 |
|
128 | | -@image_comparison(['property_collision_fill.png'], remove_text=True) |
129 | 88 | def test_property_collision_fill(): |
130 | 89 | fig, ax = plt.subplots() |
131 | | - xs = np.arange(10) |
132 | | - ys = 0.25 * xs**.5 + 2 |
133 | 90 | ax.set_prop_cycle(linewidth=[2, 3, 4, 5, 6], facecolor='bgcmy') |
| 91 | + t = range(10) |
134 | 92 | 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] |
138 | 99 |
|
139 | 100 |
|
140 | 101 | def test_valid_input_forms(): |
|
0 commit comments