7575 DateIndexFormatter - date plots with implicit x indexing.
7676
7777"""
78- import sys , re , time
78+ import sys , re , time , math
7979import locale
8080
8181import matplotlib
@@ -703,6 +703,49 @@ def mx2num(mxdates):
703703 for t in dates : print formatter (t )
704704
705705
706+ def date_ticker_factory (span , tz = None , numticks = 5 ):
707+ """
708+ Create a date locator with numticks (approx) and a date formatter
709+ for span in days. Return value is (locator, formatter)
710+
711+
712+ """
713+
714+ if span == 0 : span = 1 / 24.
715+
716+ minutes = span * 24 * 60
717+ hours = span * 24
718+ days = span
719+ weeks = span / 7.
720+ months = span / 31. # approx
721+ years = span / 365.
722+
723+ if years > numticks :
724+ locator = YearLocator (int (years / numticks ), tz = tz ) # define
725+ fmt = '%Y'
726+ elif months > numticks :
727+ locator = MonthLocator (tz = tz )
728+ fmt = '%b %Y'
729+ elif weeks > numticks :
730+ locator = WeekdayLocator (tz = tz )
731+ fmt = '%a, %b %d'
732+ elif days > numticks :
733+ locator = DayLocator (interval = int (math .ceil (days / numticks )), tz = tz )
734+ fmt = '%b %d'
735+ elif hours > numticks :
736+ locator = HourLocator (interval = int (math .ceil (hours / numticks )), tz = tz )
737+ fmt = '%H:%M\n %b %d'
738+ elif minutes > numticks :
739+ locator = MinuteLocator (interval = int (math .ceil (minutes / numticks )), tz = tz )
740+ fmt = '%H:%M:%S'
741+ else :
742+ locator = MinuteLocator (tz = tz )
743+ fmt = '%H:%M:%S'
744+
745+
746+ formatter = DateFormatter (fmt , tz = tz )
747+ return locator , formatter
748+
706749__all__ = ( 'date2num' , 'num2date' , 'drange' , 'epoch2num' ,
707750 'num2epoch' , 'mx2num' , 'DateFormatter' ,
708751 'IndexDateFormatter' , 'DateLocator' , 'RRuleLocator' ,
@@ -711,3 +754,4 @@ def mx2num(mxdates):
711754 'SecondLocator' , 'rrule' , 'MO' , 'TU' , 'WE' , 'TH' , 'FR' ,
712755 'SA' , 'SU' , 'YEARLY' , 'MONTHLY' , 'WEEKLY' , 'DAILY' ,
713756 'HOURLY' , 'MINUTELY' , 'SECONDLY' , 'relativedelta' )
757+
0 commit comments