3
3
"""
4
4
5
5
#!!! TODO: Use the fact that axis can have units to simplify the process
6
-
6
+ import datetime as pydt
7
+ from datetime import datetime
7
8
8
9
from matplotlib import pylab
9
10
from matplotlib .ticker import Formatter , Locator
17
18
18
19
from pandas .tseries .period import Period , PeriodIndex
19
20
from pandas .tseries .index import DatetimeIndex
21
+ from pandas .core .index import Index
20
22
from pandas .core .series import Series
21
23
22
24
import warnings
@@ -102,6 +104,9 @@ def tsplot(series, plotf, *args, **kwargs):
102
104
103
105
# Specialized ts plotting attributes for Axes
104
106
ax .freq = freq
107
+ xaxis = ax .get_xaxis ()
108
+ xaxis .freq = freq
109
+ xaxis .converter = DateConverter
105
110
ax .legendlabels = [kwargs .get ('label' , None )]
106
111
ax .view_interval = None
107
112
ax .date_axis_info = None
@@ -119,8 +124,8 @@ def tsplot(series, plotf, *args, **kwargs):
119
124
# if xlim still at default values, autoscale the axis
120
125
ax .autoscale_view ()
121
126
122
- left = get_datevalue (series .index [0 ], freq )
123
- right = get_datevalue (series .index [- 1 ], freq )
127
+ left = series . index [ 0 ] # get_datevalue(series.index[0], freq)
128
+ right = series . index [ - 1 ] # get_datevalue(series.index[-1], freq)
124
129
ax .set_xlim (left , right )
125
130
126
131
return plotted
@@ -130,7 +135,7 @@ def tsplot(series, plotf, *args, **kwargs):
130
135
def get_datevalue (date , freq ):
131
136
if isinstance (date , Period ):
132
137
return date .asfreq (freq ).ordinal
133
- elif isinstance (date , str ):
138
+ elif isinstance (date , ( str , datetime , pydt . date , pydt . time ) ):
134
139
return Period (date , freq ).ordinal
135
140
elif isinstance (date , (int , float )) or \
136
141
(isinstance (date , np .ndarray ) and (date .size == 1 )):
@@ -929,35 +934,12 @@ def add_yaxis(fsp=None, position='right', yscale=None, basey=10, subsy=None):
929
934
pylab .draw_if_interactive ()
930
935
return fsp_alt
931
936
932
- def set_dlim (subplot , start_date = None , end_date = None ):
933
- """
934
- Sets the date limits of the plot to ``start_date`` and ``end_date``.
935
- The dates can be given as :class:`~Period` objects, strings or
936
- integers.
937
+ class DateConverter (object ):
937
938
938
- Parameters
939
- ----------
940
- start_date : {var}
941
- Starting date of the plot. If None, the current left limit
942
- (earliest date) is used.
943
- end_date : {var}
944
- Ending date of the plot. If None, the current right limit (latest
945
- date) is used.
946
- """
947
- freq = getattr (subplot , 'freq' , None )
948
- if freq is None :
949
- raise ValueError ("Undefined frequency! Date limits can't be set!" )
950
- xleft = get_datevalue (start_date , freq )
951
- xright = get_datevalue (end_date , freq )
952
- subplot .set_xlim (xleft , xright )
953
- return (xleft , xright )
954
-
955
- def get_dlim (subplot ):
956
- """
957
- Returns the limits of the x axis as a :class:`~PeriodIndex`.
958
- """
959
- freq = getattr (subplot , 'freq' , None )
960
- xlims = subplot .get_xlim ()
961
- if freq is None :
962
- return xlims
963
- return PeriodIndex (xlims , freq = freq )
939
+ @classmethod
940
+ def convert (cls , values , units , axis ):
941
+ if isinstance (values , (int , float , str , datetime , Period )):
942
+ return get_datevalue (values , axis .freq )
943
+ if isinstance (values , Index ):
944
+ return values .map (lambda x : get_datevalue (x , axis .freq ))
945
+ return map (lambda x : get_datevalue (x , axis .freq ), values )
0 commit comments