3
3
4
4
import matplotlib .pyplot as plt
5
5
import numpy as np
6
+ from datetime import datetime
6
7
from matplotlib .container import ErrorbarContainer
7
8
from matplotlib .dates import DateConverter , num2date
8
9
from matplotlib .lines import Line2D
@@ -186,18 +187,30 @@ def labelLines(
186
187
if isinstance (xvals , tuple ) and len (xvals ) == 2 :
187
188
xmin , xmax = xvals
188
189
xscale = ax .get_xscale ()
190
+
191
+ # Convert datetime objects to numeric values for linspace/geomspace
192
+ x_is_datetime = isinstance (xmin , datetime ) or isinstance (xmax , datetime )
193
+ if x_is_datetime :
194
+ xmin = plt .matplotlib .dates .date2num (xmin )
195
+ xmax = plt .matplotlib .dates .date2num (xmax )
196
+
189
197
if xscale == "log" :
190
198
xvals = np .geomspace (xmin , xmax , len (all_lines ) + 2 )[1 :- 1 ]
191
199
else :
192
200
xvals = np .linspace (xmin , xmax , len (all_lines ) + 2 )[1 :- 1 ]
193
201
202
+ # Convert numeric values back to datetime objects
203
+ if x_is_datetime :
204
+ xvals = plt .matplotlib .dates .num2date (xvals )
205
+
194
206
# Build matrix line -> xvalue
195
207
ok_matrix = np .zeros ((len (all_lines ), len (all_lines )), dtype = bool )
196
208
197
209
for i , line in enumerate (all_lines ):
198
210
xdata , _ = normalize_xydata (line )
199
211
minx , maxx = min (xdata ), max (xdata )
200
212
for j , xv in enumerate (xvals ): # type: ignore
213
+ xv = line .convert_xunits (xv )
201
214
ok_matrix [i , j ] = minx < xv < maxx
202
215
203
216
# If some xvals do not fall in their corresponding line,
@@ -224,6 +237,8 @@ def labelLines(
224
237
# Move xlabel if it is outside valid range
225
238
xdata , _ = normalize_xydata (line )
226
239
xmin , xmax = min (xdata ), max (xdata )
240
+ xv = line .convert_xunits (xv )
241
+
227
242
if not (xmin <= xv <= xmax ):
228
243
warnings .warn (
229
244
(
0 commit comments