11import warnings
22from typing import Optional , Union
3-
3+ from datetime import timedelta
44import matplotlib .pyplot as plt
55import numpy as np
66from 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+ )
813from matplotlib .lines import Line2D
914from more_itertools import always_iterable
1015
@@ -19,6 +24,8 @@ def labelLine(
1924 label : Optional [str ] = None ,
2025 align : Optional [bool ] = None ,
2126 drop_label : bool = False ,
27+ xoffset : float = 0 ,
28+ xoffset_logspace : bool = False ,
2229 yoffset : float = 0 ,
2330 yoffset_logspace : bool = False ,
2431 outline_color : str = "auto" ,
@@ -43,6 +50,11 @@ def labelLine(
4350 drop_label : bool, optional
4451 If True, the label is consumed by the function so that subsequent
4552 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
4658 yoffset : double, optional
4759 Space to add to label's y position
4860 yoffset_logspace : bool, optional
@@ -65,6 +77,8 @@ def labelLine(
6577 x ,
6678 label = label ,
6779 align = align ,
80+ xoffset = xoffset ,
81+ xoffset_logspace = xoffset_logspace ,
6882 yoffset = yoffset ,
6983 yoffset_logspace = yoffset_logspace ,
7084 outline_color = outline_color ,
@@ -97,6 +111,7 @@ def labelLines(
97111 xvals : Optional [Union [tuple [float , float ], list [float ]]] = None ,
98112 drop_label : bool = False ,
99113 shrink_factor : float = 0.05 ,
114+ xoffsets : Union [float , list [float ]] = 0 ,
100115 yoffsets : Union [float , list [float ]] = 0 ,
101116 outline_color : str = "auto" ,
102117 outline_width : float = 5 ,
@@ -120,6 +135,9 @@ def labelLines(
120135 calls to e.g. legend do not use it anymore.
121136 shrink_factor : double, optional
122137 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.
123141 yoffsets : number or list, optional.
124142 Distance relative to the line when positioning the labels. If given a number,
125143 the same value is used for all lines.
@@ -243,20 +261,29 @@ def labelLines(
243261 converter = ax .xaxis .converter
244262 else :
245263 converter = ax .xaxis .get_converter ()
246- if isinstance (converter , DateConverter ):
264+ time_classes = (_SwitchableDateConverter , DateConverter , ConciseDateConverter )
265+ if isinstance (converter , time_classes ):
247266 xvals = [
248267 num2date (x ).replace (tzinfo = ax .xaxis .get_units ())
249268 for x in xvals # type: ignore
250269 ]
251270
252271 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
253279 try :
254280 yoffsets = [float (yoffsets )] * len (all_lines ) # type: ignore
255281 except TypeError :
256282 pass
257- for line , x , yoffset , label in zip (
283+ for line , x , xoffset , yoffset , label in zip (
258284 lab_lines ,
259285 xvals , # type: ignore
286+ xoffsets , # type: ignore
260287 yoffsets , # type: ignore
261288 labels ,
262289 ):
@@ -267,6 +294,7 @@ def labelLines(
267294 label = label ,
268295 align = align ,
269296 drop_label = drop_label ,
297+ xoffset = xoffset ,
270298 yoffset = yoffset ,
271299 outline_color = outline_color ,
272300 outline_width = outline_width ,
0 commit comments