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