@@ -182,6 +182,7 @@ def _split_eq_width(self, min, max):
182182class BinDefinition (NamedTuple ):
183183 thresholds : np .ndarray # thresholds, including the top
184184 labels : List [str ] # friendly-formatted thresholds
185+ short_labels : List [str ] # shorter labels (e.g. simplified dates)
185186 width : Union [float , None ] # widths, if uniform; otherwise None
186187 width_label : str # friendly-formatted width (e.g. '50' or '2 weeks')
187188
@@ -190,14 +191,25 @@ class BinDefinition(NamedTuple):
190191# Name of the class has to be the same to match the namedtuple name
191192# pylint: disable=function-redefined
192193class BinDefinition (BinDefinition ):
193- def __new__ (cls , thresholds , labels = "%g" , width = None , width_label = "" ):
194- if isinstance (labels , str ):
195- labels = [labels % x for x in thresholds ]
196- elif isinstance (labels , Callable ):
197- labels = [labels (x ) for x in thresholds ]
194+ def __new__ (cls , thresholds , labels = "%g" ,
195+ short_labels = None , width = None , width_label = "" ):
196+
197+ def get_labels (fmt , default = None ):
198+ if fmt is None :
199+ return default
200+ if isinstance (fmt , str ):
201+ return [fmt % x for x in thresholds ]
202+ elif isinstance (fmt , Callable ):
203+ return [fmt (x ) for x in thresholds ]
204+ else :
205+ return fmt
206+
207+ labels = get_labels (labels )
208+ short_labels = get_labels (short_labels , labels )
198209 if not width_label and width is not None :
199210 width_label = f"{ width :g} "
200- return super ().__new__ (cls , thresholds , labels , width , width_label )
211+ return super ().__new__ (
212+ cls , thresholds , labels , short_labels , width , width_label )
201213
202214 @property
203215 def start (self ) -> float :
@@ -299,7 +311,7 @@ def decimal_binnings(
299311 if min_bins <= nbins <= max_bins \
300312 and (not bins or bins [- 1 ].nbins != nbins ):
301313 bin_def = BinDefinition (mn_ + width * np .arange (nbins + 1 ),
302- label_fmt , width )
314+ label_fmt , None , width )
303315 bins .append (bin_def )
304316 return bins
305317
@@ -362,12 +374,14 @@ def _time_binnings(mn, mx, min_pts, max_pts):
362374 continue
363375 times = [time .struct_time (t + (0 , 0 , 0 )) for t in times ]
364376 thresholds = [calendar .timegm (t ) for t in times ]
365- labels = _simplified_labels ([time .strftime (fmt , t ) for t in times ])
377+ labels = [time .strftime (fmt , t ) for t in times ]
378+ short_labels = _simplified_labels (labels )
366379 if place == 2 and step >= 7 :
367380 unit_label = f"{ step // 7 } week{ 's' * (step > 7 )} "
368381 else :
369382 unit_label = f"{ step } { unit } { 's' * (step > 1 )} "
370- new_bins = BinDefinition (thresholds , labels , None , unit_label )
383+ new_bins = BinDefinition (
384+ thresholds , labels , short_labels , None , unit_label )
371385 if not bins or new_bins .nbins != bins [- 1 ].nbins :
372386 bins .append (new_bins )
373387 return bins
@@ -417,6 +431,7 @@ def _month_days(year, month,
417431
418432
419433def _simplified_labels (labels ):
434+ labels = labels [:]
420435 to_remove = "42"
421436 while True :
422437 firsts = {f for f , * _ in (lab .split () for lab in labels )}
@@ -442,8 +457,9 @@ def _unique_time_bins(unique):
442457 fmt = f'{ "%y " if times [0 ][0 ] >= 1950 else "%Y " } %b %d'
443458 fmt += " %H:%M" * (len ({t [2 :] for t in times }) > 1 )
444459 fmt += ":%S" * bool (np .all (unique % 60 == 0 ))
445- return BinDefinition (_unique_thresholds (unique ),
446- [time .strftime (fmt , x ) for x in times ])
460+ labels = [time .strftime (fmt , x ) for x in times ]
461+ short_labels = _simplified_labels (labels )
462+ return BinDefinition (_unique_thresholds (unique ), labels , short_labels )
447463
448464
449465def _unique_thresholds (unique ):
0 commit comments