File tree Expand file tree Collapse file tree 2 files changed +24
-6
lines changed Expand file tree Collapse file tree 2 files changed +24
-6
lines changed Original file line number Diff line number Diff line change 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
@@ -256,15 +261,19 @@ def labelLines(
256
261
converter = ax .xaxis .converter
257
262
else :
258
263
converter = ax .xaxis .get_converter ()
259
- if isinstance (converter , DateConverter ):
264
+ time_classes = (_SwitchableDateConverter , DateConverter , ConciseDateConverter )
265
+ if isinstance (converter , time_classes ):
260
266
xvals = [
261
267
num2date (x ).replace (tzinfo = ax .xaxis .get_units ())
262
268
for x in xvals # type: ignore
263
269
]
264
270
265
271
txts = []
266
272
try :
267
- xoffsets = [float (xoffsets )] * len (all_lines ) # type: ignore
273
+ if isinstance (xoffsets , timedelta ):
274
+ xoffsets = [xoffsets ] * len (all_lines ) # type: ignore
275
+ else :
276
+ xoffsets = [float (xoffsets )] * len (all_lines ) # type: ignore
268
277
except TypeError :
269
278
pass
270
279
try :
Original file line number Diff line number Diff line change 2
2
3
3
from typing import TYPE_CHECKING
4
4
5
+ import matplotlib .dates as mdates
5
6
import matplotlib .patheffects as patheffects
6
7
import numpy as np
8
+ from datetime import timedelta
7
9
from matplotlib .text import Text
8
10
9
11
from .utils import normalize_xydata
@@ -177,6 +179,13 @@ def _update_anchors(self):
177
179
x = self ._line .convert_xunits (self ._target_x )
178
180
xdata , ydata = normalize_xydata (self ._line )
179
181
182
+ # Convert timedelta to float if needed
183
+ if isinstance (self ._xoffset , timedelta ):
184
+ xoffset = mdates .date2num (self ._xoffset + self ._target_x ) - x
185
+ else :
186
+ xoffset = self ._xoffset
187
+
188
+ # Handle nan values
180
189
mask = np .isfinite (ydata )
181
190
if mask .sum () == 0 :
182
191
raise ValueError (f"The line { self ._line } only contains nan!" )
@@ -202,9 +211,9 @@ def _update_anchors(self):
202
211
203
212
# Apply x offset
204
213
if self ._xoffset_logspace :
205
- x *= 10 ** self . _xoffset
214
+ x *= 10 ** xoffset
206
215
else :
207
- x += self . _xoffset
216
+ x += xoffset
208
217
209
218
# Apply y offset
210
219
if self ._yoffset_logspace :
You can’t perform that action at this time.
0 commit comments