1
1
import warnings
2
2
from typing import Optional , Union
3
-
3
+ from datetime import timedelta
4
4
import matplotlib .pyplot as plt
5
5
import numpy as np
6
6
from matplotlib .container import ErrorbarContainer
7
- from matplotlib .dates import DateConverter , num2date
7
+ from matplotlib .dates import (
8
+ _SwitchableDateConverter ,
9
+ ConciseDateConverter ,
10
+ DateConverter ,
11
+ num2date ,
12
+ )
8
13
from matplotlib .lines import Line2D
9
14
from more_itertools import always_iterable
10
15
@@ -19,6 +24,8 @@ def labelLine(
19
24
label : Optional [str ] = None ,
20
25
align : Optional [bool ] = None ,
21
26
drop_label : bool = False ,
27
+ xoffset : float = 0 ,
28
+ xoffset_logspace : bool = False ,
22
29
yoffset : float = 0 ,
23
30
yoffset_logspace : bool = False ,
24
31
outline_color : str = "auto" ,
@@ -43,6 +50,11 @@ def labelLine(
43
50
drop_label : bool, optional
44
51
If True, the label is consumed by the function so that subsequent
45
52
calls to e.g. legend do not use it anymore.
53
+ xoffset : double, optional
54
+ Space to add to label's x position
55
+ xoffset_logspace : bool, optional
56
+ If True, then xoffset will be added to the label's x position in
57
+ log10 space
46
58
yoffset : double, optional
47
59
Space to add to label's y position
48
60
yoffset_logspace : bool, optional
@@ -65,6 +77,8 @@ def labelLine(
65
77
x ,
66
78
label = label ,
67
79
align = align ,
80
+ xoffset = xoffset ,
81
+ xoffset_logspace = xoffset_logspace ,
68
82
yoffset = yoffset ,
69
83
yoffset_logspace = yoffset_logspace ,
70
84
outline_color = outline_color ,
@@ -97,6 +111,7 @@ def labelLines(
97
111
xvals : Optional [Union [tuple [float , float ], list [float ]]] = None ,
98
112
drop_label : bool = False ,
99
113
shrink_factor : float = 0.05 ,
114
+ xoffsets : Union [float , list [float ]] = 0 ,
100
115
yoffsets : Union [float , list [float ]] = 0 ,
101
116
outline_color : str = "auto" ,
102
117
outline_width : float = 5 ,
@@ -120,6 +135,9 @@ def labelLines(
120
135
calls to e.g. legend do not use it anymore.
121
136
shrink_factor : double, optional
122
137
Relative distance from the edges to place closest labels. Defaults to 0.05.
138
+ xoffsets : number or list, optional.
139
+ Distance relative to the line when positioning the labels. If given a number,
140
+ the same value is used for all lines.
123
141
yoffsets : number or list, optional.
124
142
Distance relative to the line when positioning the labels. If given a number,
125
143
the same value is used for all lines.
@@ -243,20 +261,29 @@ def labelLines(
243
261
converter = ax .xaxis .converter
244
262
else :
245
263
converter = ax .xaxis .get_converter ()
246
- if isinstance (converter , DateConverter ):
264
+ time_classes = (_SwitchableDateConverter , DateConverter , ConciseDateConverter )
265
+ if isinstance (converter , time_classes ):
247
266
xvals = [
248
267
num2date (x ).replace (tzinfo = ax .xaxis .get_units ())
249
268
for x in xvals # type: ignore
250
269
]
251
270
252
271
txts = []
272
+ try :
273
+ if isinstance (xoffsets , timedelta ):
274
+ xoffsets = [xoffsets ] * len (all_lines ) # type: ignore
275
+ else :
276
+ xoffsets = [float (xoffsets )] * len (all_lines ) # type: ignore
277
+ except TypeError :
278
+ pass
253
279
try :
254
280
yoffsets = [float (yoffsets )] * len (all_lines ) # type: ignore
255
281
except TypeError :
256
282
pass
257
- for line , x , yoffset , label in zip (
283
+ for line , x , xoffset , yoffset , label in zip (
258
284
lab_lines ,
259
285
xvals , # type: ignore
286
+ xoffsets , # type: ignore
260
287
yoffsets , # type: ignore
261
288
labels ,
262
289
):
@@ -267,6 +294,7 @@ def labelLines(
267
294
label = label ,
268
295
align = align ,
269
296
drop_label = drop_label ,
297
+ xoffset = xoffset ,
270
298
yoffset = yoffset ,
271
299
outline_color = outline_color ,
272
300
outline_width = outline_width ,
0 commit comments