-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest.py
499 lines (369 loc) · 12 KB
/
test.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import numpy as np
import pytest
from matplotlib.dates import UTC, DateFormatter, DayLocator
from matplotlib.testing import setup
from .core import labelLine, labelLines
@pytest.fixture()
def setup_mpl():
setup()
plt.clf()
@pytest.mark.mpl_image_compare
def test_linspace(setup_mpl):
x = np.linspace(0, 1)
K = [1, 2, 4]
for k in K:
plt.plot(x, np.sin(k * x), label=rf"$f(x)=\sin({k} x)$")
labelLines(plt.gca().get_lines(), zorder=2.5)
plt.xlabel("$x$")
plt.ylabel("$f(x)$")
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_ylogspace(setup_mpl):
x = np.linspace(0, 1)
K = [1, 2, 4]
for k in K:
plt.plot(x, np.exp(k * x), label=rf"$f(x)=\exp({k} x)$")
plt.yscale("log")
labelLines(plt.gca().get_lines(), zorder=2.5)
plt.xlabel("$x$")
plt.ylabel("$f(x)$")
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_xlogspace(setup_mpl):
x = np.linspace(0, 10)
K = [1, 2, 4]
for k in K:
plt.plot(10**x, k * x, label=rf"$f(x)={k} x$")
plt.xscale("log")
# NOTE: depending on roundoff, the upper limit may be
# 1e11 or 1e10. See PR #155.
plt.xlim(1e0, 1e11)
labelLines(plt.gca().get_lines(), zorder=2.5)
plt.xlabel("$x$")
plt.ylabel("$f(x)$")
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_xylogspace(setup_mpl):
x = np.geomspace(0.1, 1e1)
K = np.arange(-5, 5, 2)
for k in K:
plt.plot(x, np.power(x, k), label=rf"$f(x)=x^{{{k}}}$")
plt.xscale("log")
plt.yscale("log")
# We fix the xvals to prevent out-of-range
labelLines(
plt.gca().get_lines(),
xvals=(0.1, 1e1),
zorder=2.5,
)
plt.xlabel("$x$")
plt.ylabel("$f(x)$")
plt.ylim(1e-6, 1e6)
# We need to fix the xlims to prevent funky xlims
# see PR #115
plt.xlim(0.1 / 1.2, 1e1 * 1.2)
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_align(setup_mpl):
x = np.linspace(0, 2 * np.pi)
y = np.sin(x)
lines = plt.plot(x, y, label=r"$\sin(x)$")
labelLines(lines, align=False)
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_rotation_correction(setup_mpl):
# Fix axes limits and plot line
fig, ax = plt.subplots()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
lines = plt.plot((0, 1), (0, 2), label="rescaled")
# Now label the line and THEN rescale the axes, to force label rotation
labelLine(lines[0], 0.5)
ax.set_ylim(0, 2)
return fig
@pytest.mark.mpl_image_compare
def test_vertical(setup_mpl):
x = 0.5
line = plt.axvline(x, label="axvline")
labelLine(line, x)
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_labels_range(setup_mpl):
x = np.linspace(0, 1)
plt.plot(x, np.sin(x), label=r"$\sin x$")
plt.plot(x, np.cos(x), label=r"$\cos x$")
labelLines(plt.gca().get_lines(), xvals=(0, 0.5))
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_dateaxis_naive(setup_mpl):
dates = [datetime(2018, 11, 1), datetime(2018, 11, 2), datetime(2018, 11, 3)]
plt.plot(dates, [0, 5, 3], label="apples")
plt.plot(dates, [3, 6, 2], label="banana")
ax = plt.gca()
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_major_formatter(DateFormatter("%Y-%m-%d"))
labelLines(ax.get_lines())
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_dateaxis_advanced(setup_mpl):
dates = [
datetime(2018, 11, 1, tzinfo=UTC),
datetime(2018, 11, 2, tzinfo=UTC),
datetime(2018, 11, 5, tzinfo=UTC),
datetime(2018, 11, 3, tzinfo=UTC),
]
plt.plot(dates, [0, 5, 3, 0], label="apples")
plt.plot(dates, [3, 6, 2, 1], label="banana")
ax = plt.gca()
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_major_formatter(DateFormatter("%Y-%m-%d"))
labelLines(ax.get_lines())
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_dateaxis_timedelta_xoffset(setup_mpl):
dates = [datetime(2018, 11, 1), datetime(2018, 11, 2), datetime(2018, 11, 3)]
dt = timedelta(hours=12)
plt.plot(dates, [0, 1, 2], label="apples")
plt.plot(dates, [3, 4, 5], label="banana")
ax = plt.gca()
labelLines(ax.get_lines(), xoffsets=dt)
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_polar(setup_mpl):
t = np.linspace(0, 2 * np.pi, num=128)
plt.plot(np.cos(t), np.sin(t), label="$1/1$")
plt.plot(np.cos(t), np.sin(2 * t), label="$1/2$")
plt.plot(np.cos(3 * t), np.sin(t), label="$3/1$")
ax = plt.gca()
labelLines(ax.get_lines())
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_non_uniform_and_negative_spacing(setup_mpl):
x = [1, -2, -3, 2, -4, -3]
plt.plot(x, [1, 2, 3, 4, 2, 1], ".-", label="apples")
plt.plot(x, [6, 5, 4, 2, 5, 5], "o-", label="banana")
ax = plt.gca()
labelLines(ax.get_lines())
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_errorbar(setup_mpl):
x = np.linspace(0, 1, 20)
y = x**0.5
dy = x
plt.errorbar(x, y, yerr=dy, label=r"$\sqrt{x}\pm x$")[0]
y = x**3
dy = x
plt.errorbar(x, y, yerr=dy, label=r"$x^3\pm x$")[0]
labelLines()
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_rotation(setup_mpl):
x = np.linspace(0, 2 * np.pi)
y = np.sin(x)
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
axes = axes.flatten()
axes[0].set_title("Rotated by 45")
(line,) = axes[0].plot(x, y, label=r"$\sin(x)$")
labelLine(line, np.pi, rotation=45)
axes[1].set_title("Rotated by 45\naligned explicitly off")
(line,) = axes[1].plot(x, y, label=r"$\sin(x)$")
labelLine(line, np.pi, rotation=45, align=False)
axes[2].set_title("Aligned explicitly on")
(line,) = axes[2].plot(x, y, label=r"$\sin(x)$")
labelLine(line, np.pi, align=True)
axes[3].set_title("Default")
(line,) = axes[3].plot(x, y, label=r"$\sin(x)$")
labelLine(line, np.pi)
return fig
def test_nan_failure():
x = np.array([0, 1])
y = np.array([np.nan, np.nan])
line = plt.plot(x, y, label="test")[0]
with pytest.raises(ValueError):
labelLine(line, 0.5)
@pytest.mark.mpl_image_compare
def test_nan_gaps(setup_mpl):
x = np.linspace(0, 10, 100)
x[10:30] = np.nan
for i in range(10):
y = np.sin(x + i)
plt.plot(x, y, label=f"y={i}")
labelLines(plt.gca().get_lines())
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_label_range(setup_mpl):
x = np.linspace(0, 1)
line = plt.plot(x, x**2, label="lorem ipsum")[0]
# This should fail
with pytest.raises(ValueError):
labelLine(line, -1)
with pytest.raises(ValueError):
labelLine(line, 2)
# This should work
labelLine(line, 0.5)
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_negative_spacing(setup_mpl):
x = np.linspace(1, -1)
y = x**2
line = plt.plot(x, y)[0]
# Should not throw an error
labelLine(line, 0.2, label="Test")
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_label_datetime_plot(setup_mpl):
plt.clf()
# data from the chinook database of iTunes music sales
x = np.array(
[
"2009-01-31T00:00:00.000000000",
"2009-02-28T00:00:00.000000000",
"2009-03-31T00:00:00.000000000",
"2009-04-30T00:00:00.000000000",
"2009-06-30T00:00:00.000000000",
"2009-09-30T00:00:00.000000000",
"2009-10-31T00:00:00.000000000",
"2009-11-30T00:00:00.000000000",
],
dtype="datetime64[ns]",
)
y = np.array([13.86, 14.85, 28.71, 42.57, 61.38, 76.23, 77.22, 81.18])
line = plt.plot(x, y, "-")[0]
plt.xticks(rotation=45)
# should not throw an error
xlabel = datetime(2009, 3, 15)
labelLine(line, xlabel, "USA")
plt.tight_layout()
return plt.gcf()
def test_xyoffset(setup_mpl):
x = np.linspace(0, 1)
for offset in ([-0.5, 0.5], 1, 1.2): # try lists # try int # try float
plt.clf()
ax = plt.gca()
ax.plot(x, np.sin(x) * 10, label=r"$\sin x$")
ax.plot(x, np.cos(x) * 10, label=r"$\cos x$")
lines = ax.get_lines()
labelLines(
lines,
xvals=(0.2, 0.7),
xoffsets=offset,
yoffsets=offset,
align=False,
bbox={"alpha": 0},
)
@pytest.mark.mpl_image_compare
def test_outline(setup_mpl):
x = np.linspace(-2, 2)
plt.ylim(-1, 5)
plt.xlim(-2, 2)
for dy, xlabel, w in zip(
np.linspace(-1, 1, 5),
np.linspace(-1.5, 1.5, 5),
np.linspace(0, 16, 5),
):
y = x**2 + dy
(line,) = plt.plot(x, y, label=f"width={w}")
labelLine(line, xlabel, outline_width=w, outline_color="gray")
return plt.gcf()
@pytest.mark.mpl_image_compare
def test_auto_layout(setup_mpl):
X = [[1, 2], [0, 1]]
Y = [[0, 1], [0, 1]]
lines = []
for i, (x, y) in enumerate(zip(X, Y)):
lines.extend(plt.plot(x, y, label=f"i={i}"))
labelLines(lines)
return plt.gcf()
def test_warning_out_of_range():
X = [0, 1]
Y = [0, 1]
lines = plt.plot(X, Y, label="test")
with pytest.warns(
UserWarning,
match=(
"The value at position 0 in `xvals` is outside the range of its "
"associated line"
),
):
labelLines(lines, xvals=[-1])
with pytest.warns(
UserWarning,
match=(
"The value at position 0 in `xvals` is outside the range of its "
"associated line"
),
):
labelLines(lines, xvals=[2])
@pytest.mark.mpl_image_compare
def test_errorbar_with_list(setup_mpl):
np.random.seed(1234)
fig, ax = plt.subplots()
samples = ["a", "b"]
pos = [-1, 1]
x = list(np.arange(-4, 4.1, 0.1))
ys = [list(np.random.rand(len(x))), list(np.random.rand(len(x)))]
lines = []
for sample, y in zip(samples, ys):
lines.append(ax.errorbar(x, y, yerr=0.1, label=sample)[0])
labelLines(lines, align=False, xvals=pos)
return fig
@pytest.mark.mpl_image_compare
def test_labeling_axhline(setup_mpl):
fig, ax = plt.subplots()
ax.plot([10, 12, 13], [1, 2, 3], label="plot")
ax.axhline(y=2, label="axhline")
labelLines()
return fig
@pytest.fixture
def create_plot():
fig, ax = plt.subplots()
X = [0, 1]
Y = [0, 1]
lines = (
*ax.plot(X, Y, label="label1"),
*ax.plot(X, Y), # no label
*ax.plot(X, Y, label="label2"),
)
return fig, ax, lines
def test_warning_line_labeling(create_plot):
_fig, _ax, lines = create_plot
warn_msg = "Tried to label line .*, but could not find a label for it."
with pytest.warns(UserWarning, match=warn_msg):
txts = labelLines(lines)
# Make sure only two lines have been labeled
assert len(txts) == 2
with pytest.warns(UserWarning, match=warn_msg):
txts = labelLines(lines[1:])
# Make sure only one line has been labeled
assert len(txts) == 1
def test_no_warning_line_labeling(create_plot):
_fig, _ax, lines = create_plot
txts = labelLines(lines[0:1])
assert len(txts) == 1
txts = labelLines()
assert len(txts) == 2
def test_labeling_by_axis(create_plot):
txts = labelLines()
assert len(txts) == 2
def test_label_line_input():
fig, ax = plt.subplots()
x = [0, 1]
y = x
ax.plot(x, y, label="test")
msg = "When rotation is set, align needs to be false or none was align=True"
# test non-allowed combinations of inputs: It is not
# allowed to set a rotation while aligning it with the
# lines
with pytest.raises(ValueError, match=msg):
labelLines(ax.get_lines(), align=True, rotation=0)
# test default settings
for label in labelLines(ax.get_lines()):
assert label._rotation != 0
# test setting an angle
for label in labelLines(ax.get_lines(), align=False, rotation=45):
assert label._rotation == 45
# test setting an angle, not explicitly setting align
for label in labelLines(ax.get_lines(), rotation=45):
assert label._rotation == 45