-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathtest_marker.py
More file actions
198 lines (161 loc) · 6.81 KB
/
test_marker.py
File metadata and controls
198 lines (161 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import markers
from matplotlib.path import Path
from matplotlib.testing.decorators import check_figures_equal
import pytest
def test_marker_fillstyle():
marker_style = markers.MarkerStyle(marker='o', fillstyle='none')
assert marker_style.get_fillstyle() == 'none'
assert not marker_style.is_filled()
@pytest.mark.parametrize('marker', [
'o',
'x',
'',
'None',
None,
r'$\frac{1}{2}$',
"$\u266B$",
1,
markers.TICKLEFT,
[[-1, 0], [1, 0]],
np.array([[-1, 0], [1, 0]]),
Path([[0, 0], [1, 0]], [Path.MOVETO, Path.LINETO]),
(5, 0), # a pentagon
(7, 1), # a 7-pointed star
(5, 2), # asterisk
(5, 0, 10), # a pentagon, rotated by 10 degrees
(7, 1, 10), # a 7-pointed star, rotated by 10 degrees
(5, 2, 10), # asterisk, rotated by 10 degrees
markers.MarkerStyle(),
markers.MarkerStyle('o'),
])
def test_markers_valid(marker):
# Checking this doesn't fail.
markers.MarkerStyle(marker)
@pytest.mark.parametrize('marker', [
'square', # arbitrary string
np.array([[-0.5, 0, 1, 2, 3]]), # 1D array
(1,),
(5, 3), # second parameter of tuple must be 0, 1, or 2
(1, 2, 3, 4),
])
def test_markers_invalid(marker):
with pytest.raises(ValueError):
markers.MarkerStyle(marker)
class UnsnappedMarkerStyle(markers.MarkerStyle):
"""
A MarkerStyle where the snap threshold is force-disabled.
This is used to compare to polygon/star/asterisk markers which do not have
any snap threshold set.
"""
def _recache(self):
super()._recache()
self._snap_threshold = None
@check_figures_equal()
def test_poly_marker(fig_test, fig_ref):
ax_test = fig_test.add_subplot()
ax_ref = fig_ref.add_subplot()
# Note, some reference sizes must be different because they have unit
# *length*, while polygon markers are inscribed in a circle of unit
# *radius*. This introduces a factor of np.sqrt(2), but since size is
# squared, that becomes 2.
size = 20**2
# Squares
ax_test.scatter([0], [0], marker=(4, 0, 45), s=size)
ax_ref.scatter([0], [0], marker='s', s=size/2)
# Diamonds, with and without rotation argument
ax_test.scatter([1], [1], marker=(4, 0), s=size)
ax_ref.scatter([1], [1], marker=UnsnappedMarkerStyle('D'), s=size/2)
ax_test.scatter([1], [1.5], marker=(4, 0, 0), s=size)
ax_ref.scatter([1], [1.5], marker=UnsnappedMarkerStyle('D'), s=size/2)
# Pentagon, with and without rotation argument
ax_test.scatter([2], [2], marker=(5, 0), s=size)
ax_ref.scatter([2], [2], marker=UnsnappedMarkerStyle('p'), s=size)
ax_test.scatter([2], [2.5], marker=(5, 0, 0), s=size)
ax_ref.scatter([2], [2.5], marker=UnsnappedMarkerStyle('p'), s=size)
# Hexagon, with and without rotation argument
ax_test.scatter([3], [3], marker=(6, 0), s=size)
ax_ref.scatter([3], [3], marker='h', s=size)
ax_test.scatter([3], [3.5], marker=(6, 0, 0), s=size)
ax_ref.scatter([3], [3.5], marker='h', s=size)
# Rotated hexagon
ax_test.scatter([4], [4], marker=(6, 0, 30), s=size)
ax_ref.scatter([4], [4], marker='H', s=size)
# Octagons
ax_test.scatter([5], [5], marker=(8, 0, 22.5), s=size)
ax_ref.scatter([5], [5], marker=UnsnappedMarkerStyle('8'), s=size)
ax_test.set(xlim=(-0.5, 5.5), ylim=(-0.5, 5.5))
ax_ref.set(xlim=(-0.5, 5.5), ylim=(-0.5, 5.5))
def test_star_marker():
# We don't really have a strict equivalent to this marker, so we'll just do
# a smoke test.
size = 20**2
fig, ax = plt.subplots()
ax.scatter([0], [0], marker=(5, 1), s=size)
ax.scatter([1], [1], marker=(5, 1, 0), s=size)
ax.set(xlim=(-0.5, 0.5), ylim=(-0.5, 1.5))
# The asterisk marker is really a star with 0-size inner circle, so the ends
# are corners and get a slight bevel. The reference markers are just singular
# lines without corners, so they have no bevel, and we need to add a slight
# tolerance.
@check_figures_equal(tol=1.45)
def test_asterisk_marker(fig_test, fig_ref, request):
ax_test = fig_test.add_subplot()
ax_ref = fig_ref.add_subplot()
# Note, some reference sizes must be different because they have unit
# *length*, while asterisk markers are inscribed in a circle of unit
# *radius*. This introduces a factor of np.sqrt(2), but since size is
# squared, that becomes 2.
size = 20**2
def draw_ref_marker(y, style, size):
# As noted above, every line is doubled. Due to antialiasing, these
# doubled lines make a slight difference in the .png results.
ax_ref.scatter([y], [y], marker=UnsnappedMarkerStyle(style), s=size)
if request.getfixturevalue('ext') == 'png':
ax_ref.scatter([y], [y], marker=UnsnappedMarkerStyle(style),
s=size)
# Plus
ax_test.scatter([0], [0], marker=(4, 2), s=size)
draw_ref_marker(0, '+', size)
ax_test.scatter([0.5], [0.5], marker=(4, 2, 0), s=size)
draw_ref_marker(0.5, '+', size)
# Cross
ax_test.scatter([1], [1], marker=(4, 2, 45), s=size)
draw_ref_marker(1, 'x', size/2)
ax_test.set(xlim=(-0.5, 1.5), ylim=(-0.5, 1.5))
ax_ref.set(xlim=(-0.5, 1.5), ylim=(-0.5, 1.5))
@check_figures_equal()
def test_marker_clipping(fig_ref, fig_test):
# Plotting multiple markers can trigger different optimized paths in
# backends, so compare single markers vs multiple to ensure they are
# clipped correctly.
marker_count = len(markers.MarkerStyle.markers)
marker_size = 50
ncol = 7
nrow = marker_count // ncol + 1
width = 2 * marker_size * ncol
height = 2 * marker_size * nrow * 2
fig_ref.set_size_inches((width / fig_ref.dpi, height / fig_ref.dpi))
ax_ref = fig_ref.add_axes([0, 0, 1, 1])
fig_test.set_size_inches((width / fig_test.dpi, height / fig_ref.dpi))
ax_test = fig_test.add_axes([0, 0, 1, 1])
for i, marker in enumerate(markers.MarkerStyle.markers):
x = i % ncol
y = i // ncol * 2
# Singular markers per call.
ax_ref.plot([x, x], [y, y + 1], c='k', linestyle='-', lw=3)
ax_ref.plot(x, y, c='k',
marker=marker, markersize=marker_size, markeredgewidth=10,
fillstyle='full', markerfacecolor='white')
ax_ref.plot(x, y + 1, c='k',
marker=marker, markersize=marker_size, markeredgewidth=10,
fillstyle='full', markerfacecolor='white')
# Multiple markers in a single call.
ax_test.plot([x, x], [y, y + 1], c='k', linestyle='-', lw=3,
marker=marker, markersize=marker_size, markeredgewidth=10,
fillstyle='full', markerfacecolor='white')
ax_ref.set(xlim=(-0.5, ncol), ylim=(-0.5, 2 * nrow))
ax_test.set(xlim=(-0.5, ncol), ylim=(-0.5, 2 * nrow))
ax_ref.axis('off')
ax_test.axis('off')