@@ -806,163 +806,134 @@ def __call__(self, x, pos=None):
806
806
fmt = self .defaultfmt
807
807
self ._formatter = DateFormatter (fmt , self ._tz )
808
808
tickdate = []
809
-
809
+ tickdatetime = []
810
810
for nn , tick in enumerate (ticks ):
811
- tickdate += [num2date (tick )]
811
+ tickdate += [num2date (tick ).timetuple ()]
812
+ tickdatetime += [num2date (tick )]
812
813
yearfmt = [self ._yearfmt ] * len (tickdate )
813
814
monthfmt = [self ._monthfmt ] * len (tickdate )
814
815
dayfmt = [self ._dayfmt ] * len (tickdate )
815
816
hourfmt = [self ._hourfmt + ':' + self ._minutefmt ] * len (tickdate )
816
817
secondfmt = [self ._secondfmt ] * len (tickdate )
817
818
microsecondfmt = [self ._microsecondfmt ] * len (tickdate )
818
819
819
- # start at year....
820
+ # try w/ tupples:
821
+ # basic algoritthm:
822
+ # 1) only display a part of the date if it changes over the
823
+ # ticks.
824
+ # 2) don't display the smaller part of the date if:
825
+ # it is always the same or if it is the start of the
826
+ # year, month, day etc.
827
+ level = 5
820
828
diffed = False
821
- for nn in range (1 , len (tickdate )):
822
- if tickdate [nn ].year == tickdate [nn - 1 ].year :
823
- yearfmt [nn ] = ''
824
- else :
825
- diffed = True
826
- if not diffed :
827
- offset_fmt += yearfmt [0 ]
828
- if not diffed or tickdate [0 ].month != 1 :
829
- yearfmt [0 ] = ''
830
-
831
- # remove extra months....
832
- if not diffed :
833
- diffed = False
834
- for nn in range (1 , len (tickdate )):
835
- if (tickdate [nn ].month == tickdate [nn - 1 ].month
836
- and yearfmt [nn ] == '' ):
837
- monthfmt [nn ] = ''
838
- else :
839
- diffed = True
840
- if not diffed :
841
- offset_fmt += '-' + monthfmt [0 ]
842
- monthfmt [0 ] = ''
843
- if tickdate [0 ].day != 1 :
844
- monthfmt [0 ] = ''
845
- else :
846
- # we have more than one year, so remove Januarys because
847
- # they are labeled.
848
- for nn in range (0 , len (tickdate )):
849
- if tickdate [nn ].month == 1 :
850
- monthfmt [nn ] = ''
851
-
852
- # remove extra days:
853
- if not diffed :
854
- # we are all in one month...
855
- diffed = False
829
+ # fmt for most ticks at this level
830
+ fmts = [self ._yearfmt , self ._monthfmt , self ._dayfmt ,
831
+ self ._hourfmt + ':' + self ._minutefmt ,
832
+ self ._hourfmt + ':' + self ._minutefmt ,
833
+ self ._secondfmt + '.' + self ._microsecondfmt ,
834
+ self ._secondfmt + '.' + self ._microsecondfmt ]
835
+ # fmt for zeros ticks at this level. These are
836
+ # ticks that should be labeled w/ the level above.
837
+ zerofmts = ['' , self ._yearfmt , self ._monthfmt ,
838
+ self ._monthfmt + '-' + self ._dayfmt ,
839
+ self ._hourfmt + ':' + self ._minutefmt ,
840
+ self ._hourfmt + ':' + self ._minutefmt ,
841
+ self ._secondfmt ]
842
+ # offset fmt are for the offset in the upper left of the
843
+ # or lower right of the axis.
844
+ offsetfmts = ['' ,
845
+ self ._yearfmt ,
846
+ self ._yearfmt + '-' + self ._monthfmt ,
847
+ self ._yearfmt + '-' + self ._monthfmt + '-' + \
848
+ self ._dayfmt ,
849
+ self ._yearfmt + '-' + self ._monthfmt + '-' \
850
+ + self ._dayfmt ,
851
+ self ._monthfmt + '-' + \
852
+ self ._dayfmt + ' ' + self ._hourfmt + \
853
+ ':' + self ._minutefmt ,
854
+ self ._monthfmt + '-' + \
855
+ self ._dayfmt + ' ' + self ._hourfmt + \
856
+ ':' + self ._minutefmt ,]
857
+ # zoffset fmt are for the offset if there was a zero in
858
+ # the ticks. So this has a bit less info
859
+ zoffsetfmts = ['' ,
860
+ self ._yearfmt ,
861
+ self ._yearfmt ,
862
+ self ._yearfmt ,
863
+ self ._yearfmt + '-' + self ._monthfmt + '-' \
864
+ + self ._dayfmt ,
865
+ self ._monthfmt + '-' + \
866
+ self ._dayfmt + ' ' + self ._hourfmt + \
867
+ ':' + self ._minutefmt ,
868
+ self ._monthfmt + '-' + \
869
+ self ._dayfmt + ' ' + self ._hourfmt + \
870
+ ':' + self ._minutefmt ,]
871
+
872
+ # detremine the level we will label at:
873
+ while not diffed and level >= 0 :
856
874
for nn in range (1 , len (tickdate )):
857
- if (tickdate [nn ].day == tickdate [nn - 1 ].day
858
- and monthfmt [nn ] == '' ):
859
- dayfmt [nn ] = ''
875
+ if level < 6 :
876
+ if (not tickdate [nn - 1 ][level ] ==
877
+ tickdate [nn ][level ]):
878
+ diffed = True
860
879
else :
861
- diffed = True
880
+ if (tickdate [nn ][level ] -
881
+ tickdate [nn - 1 ][level ] > 2 ):
882
+ diffed = True
862
883
if not diffed :
863
- offset_fmt += '-' + dayfmt [0 ]
864
- if not diffed or tickdate [0 ].hour != 0 :
865
- dayfmt [0 ] = ''
866
- else :
867
- for nn in range (0 , len (tickdate )):
868
- if tickdate [nn ].day == 1 :
869
- dayfmt [nn ] = ''
870
- if (tickdate [nn ].day == tickdate [nn - 1 ].day ):
871
- dayfmt [nn ] = ''
872
-
873
- # remove extra seconds if all zero:
874
- remove = True
875
- for nn in range (0 , len (tickdate )):
876
- if tickdate [nn ].second != 0 :
877
- remove = False
878
- if remove :
879
- for nn in range (0 , len (tickdate )):
880
- secondfmt [nn ] = ''
881
- else :
882
- for nn in range (0 , len (tickdate )):
883
- if tickdate [nn ].second == 0 :
884
- secondfmt [nn ] = ''
885
-
886
- # remove extra datetime.microsecond if all zero:
887
- maxlen = 0
888
- oldremove = remove
889
- remove = True
890
- if not oldremove :
891
- for nn in range (0 , len (tickdate )):
892
- if tickdate [nn ].microsecond != 0 :
893
- remove = False
894
- fullst = '%d' % tickdate [nn ].microsecond
895
- maxlen = max (maxlen , len (fullst .rstrip ('0' )))
896
- if remove :
897
- for nn in range (0 , len (tickdate )):
898
- microsecondfmt [nn ] = ''
899
-
900
- # remove time
884
+ level -= 1
901
885
if not diffed :
902
- diffed = False
903
- for nn in range (1 , len (tickdate )):
904
- if (tickdate [nn ].hour == tickdate [nn - 1 ].hour
905
- and tickdate [nn ].minute ==
906
- tickdate [nn - 1 ].minute ):
907
- hourfmt [nn ] = ''
908
- else :
909
- diffed = True
910
- if not diffed and tickdate [0 ].second != 0 :
911
- offset_fmt += ' ' + hourfmt [0 ]
912
- hourfmt [0 ] = ''
913
- else :
914
- remove = True
915
- for nn in range (0 , len (tickdate )):
916
- if (tickdate [nn ].hour != 0 or tickdate [nn ].minute != 0 ):
917
- remove = False
918
- if remove :
919
- for nn in range (0 , len (tickdate )):
920
- hourfmt [nn ] = ''
921
- else :
922
- for nn in range (0 , len (tickdate )):
923
- if (tickdate [nn ].hour == 0 and
924
- tickdate [nn ].minute == 0 ):
925
- hourfmt [nn ] = ''
926
- monthfmt [nn ] = self ._monthfmt
927
-
928
-
886
+ level = 6
929
887
888
+ # level is the basic level we will label at.
889
+ # now loop through and decide the actual ticklabels
890
+ zerovals = [0 , 1 , 1 , 0 , 0 , 0 , 0 ]
930
891
ticknew = []
892
+ zeroed = False
931
893
for nn in range (len (tickdate )):
932
- fmt = ''
933
- if not yearfmt [nn ] == '' :
934
- fmt = yearfmt [nn ]
935
- if not monthfmt [nn ] == '' :
936
- fmt += '-'
937
- if not monthfmt [nn ] == '' :
938
- fmt += monthfmt [nn ]
939
- if not dayfmt [nn ] == '' :
940
- fmt += '-'
941
- if not dayfmt [nn ] == '' :
942
- fmt += dayfmt [nn ]
943
- if not hourfmt [nn ] == '' :
944
- fmt += ' '
945
- elif not monthfmt [nn ] == '' :
946
- fmt += ' '
947
- if not hourfmt [nn ] == '' :
948
- fmt += hourfmt [nn ]
949
- if not secondfmt [nn ] == '' :
950
- fmt += ':'
951
- if not secondfmt [nn ] == '' :
952
- fmt += secondfmt [nn ]
953
- if not microsecondfmt [nn ] == '' :
954
- fmt += '.' + microsecondfmt [nn ]
955
-
956
- newtickla = tickdate [nn ].strftime (fmt )
957
- if not microsecondfmt [nn ] == '' :
958
- newtickla = newtickla [:- (6 - maxlen )]
894
+ if tickdate [nn ][level ] == zerovals [level ]:
895
+ fmt = zerofmts [level ]
896
+ zeroed = True
897
+ else :
898
+ fmt = fmts [level ]
899
+ newtickla = tickdatetime [nn ].strftime (fmt )
959
900
ticknew += [newtickla ]
960
901
902
+ # special handling of seconds and microseconds:
903
+ # strip extra zeros and decimal if possible...
904
+ if level >= 5 :
905
+ stop = False
906
+ ind = 8
907
+ while not stop and ind > 1 :
908
+ for nn in range (len (ticknew )):
909
+ # ==9 is full ss.mmmmmmm
910
+ # format tickstring. Other tickstrings
911
+ # are zeros with appropriate info for the
912
+ # zero.
913
+ if (len (ticknew [nn ]) == 9 and
914
+ ticknew [nn ][ind ] != '0' ):
915
+ stop = True
916
+ ind = ind - 1
917
+ if ind == 1 :
918
+ # strip the decimal as well if we got to here.
919
+ ind = 0
920
+ for nn in range (len (ticknew )):
921
+ if len (ticknew [nn ]) == 9 :
922
+ ticknew [nn ] = ticknew [nn ][:ind + 2 ]
923
+
961
924
result = ticknew [pos ]
962
925
self ._oldticks = ticks
963
926
self ._oldlabels = ticknew
927
+
928
+ # set the offset string:
964
929
if self .show_offset :
965
- self .offset_string = tickdate [- 1 ].strftime (offset_fmt )
930
+ if not zeroed :
931
+ self .offset_string = tickdatetime [- 1 ].strftime (
932
+ offsetfmts [level ])
933
+ else :
934
+ self .offset_string = tickdatetime [- 1 ].strftime (
935
+ zoffsetfmts [level ])
936
+
966
937
result = self ._oldlabels [pos ]
967
938
else :
968
939
result = self ._formatter (x , pos )
0 commit comments