9
9
10
10
from matplotlib import pylab
11
11
from pandas .tseries .period import Period
12
+ import numpy as np
13
+
12
14
from pandas .tseries .offsets import DateOffset
13
15
import pandas .tseries .frequencies as frequencies
14
16
from pandas .tseries .index import DatetimeIndex
@@ -41,18 +43,14 @@ def tsplot(series, plotf, ax=None, **kwargs):
41
43
import matplotlib .pyplot as plt
42
44
ax = plt .gca ()
43
45
44
- freq , series = _maybe_resample (series , ax , kwargs )
45
-
46
- # Set ax with freq info
47
- _decorate_axes (ax , freq , kwargs )
46
+ series = _maybe_resample (series , ax , kwargs )
48
47
ax ._plot_data .append ((series , plotf , kwargs ))
49
48
lines = plotf (ax , series .index ._mpl_repr (), series .values , ** kwargs )
50
49
51
50
# set date formatter, locators and rescale limits
52
51
format_dateaxis (ax , ax .freq )
53
52
return lines
54
53
55
-
56
54
def _maybe_resample (series , ax , kwargs ):
57
55
# resample against axes freq if necessary
58
56
freq , ax_freq = _get_freq (ax , series )
@@ -75,11 +73,20 @@ def _maybe_resample(series, ax, kwargs):
75
73
series = getattr (series .resample (ax_freq ), how )().dropna ()
76
74
freq = ax_freq
77
75
elif frequencies .is_subperiod (freq , ax_freq ) or _is_sub (freq , ax_freq ):
78
- _upsample_others (ax , freq , kwargs )
76
+ _upsample_others (ax , freq )
79
77
ax_freq = freq
80
78
else : # pragma: no cover
81
79
raise ValueError ('Incompatible frequency conversion' )
82
- return freq , series
80
+
81
+ # Set ax with freq info
82
+ _decorate_axes (ax , freq )
83
+ # digging deeper
84
+ if hasattr (ax , 'left_ax' ):
85
+ _decorate_axes (ax .left_ax , freq )
86
+ elif hasattr (ax , 'right_ax' ):
87
+ _decorate_axes (ax .right_ax , freq )
88
+
89
+ return series
83
90
84
91
85
92
def _is_sub (f1 , f2 ):
@@ -92,81 +99,112 @@ def _is_sup(f1, f2):
92
99
(f2 .startswith ('W' ) and frequencies .is_superperiod (f1 , 'D' )))
93
100
94
101
95
- def _upsample_others (ax , freq , kwargs ):
96
- legend = ax .get_legend ()
97
- lines , labels = _replot_ax (ax , freq , kwargs )
98
- _replot_ax (ax , freq , kwargs )
102
+ def _get_plot_func (plotf ):
103
+ """ get actual function when plotf is specified with str """
104
+ # for tsplot
105
+ if isinstance (plotf , compat .string_types ):
106
+ from pandas .tools .plotting import _plot_klass
107
+ plotf = _plot_klass [plotf ]._plot
108
+ return plotf
109
+
110
+
111
+ def _upsample_others (ax , freq ):
99
112
100
- other_ax = None
113
+ def _replot (ax ):
114
+ data = getattr (ax , '_plot_data' , None )
115
+ if data is None :
116
+ return
117
+
118
+ # preserve legend
119
+ leg = ax .get_legend ()
120
+ handles , labels = ax .get_legend_handles_labels ()
121
+
122
+ ax ._plot_data = []
123
+ ax .clear ()
124
+ _decorate_axes (ax , freq )
125
+
126
+ for series , plotf , kwds in data :
127
+ series = series .copy ()
128
+ idx = series .index .asfreq (freq , how = 's' )
129
+ series .index = idx
130
+ ax ._plot_data .append ((series , plotf , kwds ))
131
+
132
+ plotf = _get_plot_func (plotf )
133
+ plotf (ax , series .index ._mpl_repr (), series .values , ** kwds )
134
+
135
+
136
+ if leg is not None :
137
+ ax .legend (handles , labels , title = leg .get_title ().get_text ())
138
+
139
+ _replot (ax )
101
140
if hasattr (ax , 'left_ax' ):
102
- other_ax = ax .left_ax
103
- if hasattr (ax , 'right_ax' ):
104
- other_ax = ax .right_ax
141
+ _replot ( ax .left_ax )
142
+ elif hasattr (ax , 'right_ax' ):
143
+ _replot ( ax .right_ax )
105
144
106
- if other_ax is not None :
107
- rlines , rlabels = _replot_ax (other_ax , freq , kwargs )
108
- lines .extend (rlines )
109
- labels .extend (rlabels )
110
145
111
- if (legend is not None and kwargs .get ('legend' , True ) and
112
- len (lines ) > 0 ):
113
- title = legend .get_title ().get_text ()
114
- if title == 'None' :
115
- title = None
116
- ax .legend (lines , labels , loc = 'best' , title = title )
146
+ def _replot_x_compat (ax ):
117
147
148
+ def _replot (ax ):
149
+ data = getattr (ax , '_plot_data' , None )
150
+ if data is None :
151
+ return
118
152
119
- def _replot_ax (ax , freq , kwargs ):
120
- data = getattr (ax , '_plot_data' , None )
153
+ # preserve legend
154
+ leg = ax .get_legend ()
155
+ handles , labels = ax .get_legend_handles_labels ()
121
156
122
- # clear current axes and data
123
- ax ._plot_data = []
124
- ax .clear ()
157
+ ax ._plot_data = None
158
+ ax .clear ()
125
159
126
- _decorate_axes (ax , freq , kwargs )
160
+ _decorate_axes (ax , None )
127
161
128
- lines = []
129
- labels = []
130
- if data is not None :
131
162
for series , plotf , kwds in data :
132
- series = series .copy ()
133
- idx = series .index .asfreq (freq , how = 'S' )
163
+ idx = series .index .to_timestamp (how = 's' )
134
164
series .index = idx
135
- ax ._plot_data .append ((series , plotf , kwds ))
136
165
137
- # for tsplot
138
- if isinstance (plotf , compat .string_types ):
139
- from pandas .tools .plotting import _plot_klass
140
- plotf = _plot_klass [plotf ]._plot
166
+ plotf = _get_plot_func (plotf )
167
+ plotf (ax , series .index ._mpl_repr (), series , ** kwds )
141
168
142
- lines .append (plotf (ax , series .index ._mpl_repr (),
143
- series .values , ** kwds )[0 ])
144
- labels .append (pprint_thing (series .name ))
169
+ if leg is not None :
170
+ ax .legend (handles , labels , title = leg .get_title ().get_text ())
145
171
146
- return lines , labels
172
+ _replot (ax )
173
+ if hasattr (ax , 'left_ax' ):
174
+ _replot (ax .left_ax )
175
+ elif hasattr (ax , 'right_ax' ):
176
+ _replot (ax .right_ax )
147
177
148
178
149
- def _decorate_axes (ax , freq , kwargs ):
179
+ def _decorate_axes (ax , freq ):
150
180
"""Initialize axes for time-series plotting"""
151
181
if not hasattr (ax , '_plot_data' ):
152
182
ax ._plot_data = []
153
183
154
184
ax .freq = freq
155
185
xaxis = ax .get_xaxis ()
156
186
xaxis .freq = freq
157
- if not hasattr (ax , 'legendlabels' ):
158
- ax .legendlabels = [kwargs .get ('label' , None )]
159
- else :
160
- ax .legendlabels .append (kwargs .get ('label' , None ))
161
187
ax .view_interval = None
162
188
ax .date_axis_info = None
163
189
164
190
165
- def _get_freq (ax , series ):
191
+ def _get_index_freq (data ):
192
+ freq = getattr (data .index , 'freq' , None )
193
+ if freq is None :
194
+ freq = getattr (data .index , 'inferred_freq' , None )
195
+ if freq == 'B' :
196
+ weekdays = np .unique (data .index .dayofweek )
197
+ if (5 in weekdays ) or (6 in weekdays ):
198
+ freq = None
199
+ return freq
200
+
201
+
202
+ def _get_freq (ax , data ):
166
203
# get frequency from data
167
- freq = getattr (series .index , 'freq' , None )
204
+ freq = getattr (data .index , 'freq' , None )
205
+
168
206
if freq is None :
169
- freq = getattr (series .index , 'inferred_freq' , None )
207
+ freq = getattr (data .index , 'inferred_freq' , None )
170
208
171
209
ax_freq = getattr (ax , 'freq' , None )
172
210
if ax_freq is None :
@@ -175,17 +213,17 @@ def _get_freq(ax, series):
175
213
elif hasattr (ax , 'right_ax' ):
176
214
ax_freq = getattr (ax .right_ax , 'freq' , None )
177
215
178
- # use axes freq if no data freq
179
- if freq is None :
180
- freq = ax_freq
216
+ if freq is not None :
217
+ # get the period frequency
218
+ if isinstance (freq , DateOffset ):
219
+ freq = freq .rule_code
220
+ else :
221
+ freq = frequencies .get_base_alias (freq )
181
222
182
- # get the period frequency
183
- if isinstance (freq , DateOffset ):
184
- freq = freq .rule_code
185
- else :
186
- freq = frequencies .get_base_alias (freq )
223
+ if freq is None :
224
+ raise ValueError ('Could not get frequency alias for plotting' )
225
+ freq = frequencies .get_period_alias (freq )
187
226
188
- freq = frequencies .get_period_alias (freq )
189
227
return freq , ax_freq
190
228
191
229
0 commit comments