1
- import warnings
2
1
from datetime import datetime
3
2
4
3
import matplotlib .pyplot as plt
5
4
import numpy as np
6
5
import pytest
7
6
from matplotlib .dates import UTC , DateFormatter , DayLocator
8
7
from matplotlib .testing import setup
9
- from numpy .testing import assert_raises
10
8
11
9
from .core import labelLine , labelLines
12
10
@@ -203,22 +201,22 @@ def test_nan_warning():
203
201
204
202
line = plt .plot (x , y , label = "test" )[0 ]
205
203
206
- with warnings .catch_warnings (record = True ) as w :
204
+ warn_msg = (
205
+ ".* could not be annotated due to `nans` values. "
206
+ "Consider using another location via the `x` argument."
207
+ )
208
+ with pytest .warns (UserWarning , match = warn_msg ):
207
209
labelLine (line , 0.5 )
208
- assert issubclass (w [- 1 ].category , UserWarning )
209
- assert "could not be annotated" in str (w [- 1 ].message )
210
210
211
- with warnings .catch_warnings (record = True ) as w :
212
- labelLine (line , 2.5 )
213
- assert len (w ) == 0
211
+ labelLine (line , 2.5 )
214
212
215
213
216
214
def test_nan_failure ():
217
215
x = np .array ([0 , 1 ])
218
216
y = np .array ([np .nan , np .nan ])
219
217
220
218
line = plt .plot (x , y , label = "test" )[0 ]
221
- with assert_raises (Exception ):
219
+ with pytest . raises (Exception ):
222
220
labelLine (line , 0.5 )
223
221
224
222
@@ -228,9 +226,9 @@ def test_label_range(setupMpl):
228
226
line = plt .plot (x , x ** 2 , label = "lorem ipsum" )[0 ]
229
227
230
228
# This should fail
231
- with assert_raises (Exception ):
229
+ with pytest . raises (Exception ):
232
230
labelLine (line , - 1 )
233
- with assert_raises (Exception ):
231
+ with pytest . raises (Exception ):
234
232
labelLine (line , 2 )
235
233
236
234
# This should work
@@ -365,3 +363,44 @@ def test_errorbars(setupMpl):
365
363
366
364
labelLines (ax .get_lines (), align = False , xvals = pos )
367
365
return fig
366
+
367
+
368
+ @pytest .fixture
369
+ def create_plot ():
370
+ fig , ax = plt .subplots ()
371
+ X = [0 , 1 ]
372
+ Y = [0 , 1 ]
373
+
374
+ lines = (
375
+ * ax .plot (X , Y , label = "label1" ),
376
+ * ax .plot (X , Y ), # no label
377
+ * ax .plot (X , Y , label = "label2" ),
378
+ )
379
+ return fig , ax , lines
380
+
381
+
382
+ def test_warning_line_labeling (create_plot ):
383
+ _fig , _ax , lines = create_plot
384
+
385
+ warn_msg = "Tried to label line .*, but could not find a label for it."
386
+ with pytest .warns (UserWarning , match = warn_msg ):
387
+ txts = labelLines (lines )
388
+ # Make sure only two lines have been labeled
389
+ assert len (txts ) == 2
390
+
391
+ with pytest .warns (UserWarning , match = warn_msg ):
392
+ txts = labelLines (lines [1 :])
393
+ # Make sure only one line has been labeled
394
+ assert len (txts ) == 1
395
+
396
+
397
+ def test_no_warning_line_labeling (create_plot ):
398
+ _fig , _ax , lines = create_plot
399
+
400
+ txts = labelLines (lines [0 :1 ])
401
+ assert len (txts ) == 1
402
+
403
+
404
+ def test_labeling_by_axis (create_plot ):
405
+ txts = labelLines ()
406
+ assert len (txts ) == 2
0 commit comments