<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from matplotlib import pylab <EOL> from pandas . tseries . period import Period <EOL> from pandas . tseries . offsets import DateOffset <EOL> import pandas . tseries . frequencies as frequencies <EOL> from pandas . tseries . index import DatetimeIndex <EOL> from pandas . formats . printing import pprint_thing <EOL> import pandas . compat as compat <EOL> from pandas . tseries . converter import ( TimeSeries_DateLocator , <EOL> TimeSeries_DateFormatter ) <EOL> def tsplot ( series , plotf , ax = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if ax is None : <EOL> import matplotlib . pyplot as plt <EOL> ax = plt . gca ( ) <EOL> freq , series = _maybe_resample ( series , ax , kwargs ) <EOL> _decorate_axes ( ax , freq , kwargs ) <EOL> ax . _plot_data . append ( ( series , plotf , kwargs ) ) <EOL> lines = plotf ( ax , series . index . _mpl_repr ( ) , series . values , ** kwargs ) <EOL> format_dateaxis ( ax , ax . freq ) <EOL> return lines <EOL> def _maybe_resample ( series , ax , kwargs ) : <EOL> freq , ax_freq = _get_freq ( ax , series ) <EOL> if freq is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if isinstance ( series . index , DatetimeIndex ) : <EOL> series = series . to_period ( freq = freq ) <EOL> if ax_freq is not None and freq != ax_freq : <EOL> if frequencies . is_superperiod ( freq , ax_freq ) : <EOL> series = series . copy ( ) <EOL> series . index = series . index . asfreq ( ax_freq , how = '<STR_LIT:s>' ) <EOL> freq = ax_freq <EOL> elif _is_sup ( freq , ax_freq ) : <EOL> how = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> series = getattr ( series . resample ( '<STR_LIT:D>' ) , how ) ( ) . dropna ( ) <EOL> series = getattr ( series . resample ( ax_freq ) , how ) ( ) . dropna ( ) <EOL> freq = ax_freq <EOL> elif frequencies . is_subperiod ( freq , ax_freq ) or _is_sub ( freq , ax_freq ) : <EOL> _upsample_others ( ax , freq , kwargs ) <EOL> ax_freq = freq <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return freq , series <EOL> def _is_sub ( f1 , f2 ) : <EOL> return ( ( f1 . startswith ( '<STR_LIT>' ) and frequencies . is_subperiod ( '<STR_LIT:D>' , f2 ) ) or <EOL> ( f2 . startswith ( '<STR_LIT>' ) and frequencies . is_subperiod ( f1 , '<STR_LIT:D>' ) ) ) <EOL> def _is_sup ( f1 , f2 ) : <EOL> return ( ( f1 . startswith ( '<STR_LIT>' ) and frequencies . is_superperiod ( '<STR_LIT:D>' , f2 ) ) or <EOL> ( f2 . startswith ( '<STR_LIT>' ) and frequencies . is_superperiod ( f1 , '<STR_LIT:D>' ) ) ) <EOL> def _upsample_others ( ax , freq , kwargs ) : <EOL> legend = ax . get_legend ( ) <EOL> lines , labels = _replot_ax ( ax , freq , kwargs ) <EOL> _replot_ax ( ax , freq , kwargs ) <EOL> other_ax = None <EOL> if hasattr ( ax , '<STR_LIT>' ) : <EOL> other_ax = ax . left_ax <EOL> if hasattr ( ax , '<STR_LIT>' ) : <EOL> other_ax = ax . right_ax <EOL> if other_ax is not None : <EOL> rlines , rlabels = _replot_ax ( other_ax , freq , kwargs ) <EOL> lines . extend ( rlines ) <EOL> labels . extend ( rlabels ) <EOL> if ( legend is not None and kwargs . get ( '<STR_LIT>' , True ) and <EOL> len ( lines ) > <NUM_LIT:0> ) : <EOL> title = legend . get_title ( ) . get_text ( ) <EOL> if title == '<STR_LIT:None>' : <EOL> title = None <EOL> ax . legend ( lines , labels , loc = '<STR_LIT>' , title = title ) <EOL> def _replot_ax ( ax , freq , kwargs ) : <EOL> data = getattr ( ax , '<STR_LIT>' , None ) <EOL> ax . _plot_data = [ ] <EOL> ax . clear ( ) <EOL> _decorate_axes ( ax , freq , kwargs ) <EOL> lines = [ ] <EOL> labels = [ ] <EOL> if data is not None : <EOL> for series , plotf , kwds in data : <EOL> series = series . copy ( ) <EOL> idx = series . index . asfreq ( freq , how = '<STR_LIT:S>' ) <EOL> series . index = idx <EOL> ax . _plot_data . append ( ( series , plotf , kwds ) ) <EOL> if isinstance ( plotf , compat . string_types ) : <EOL> from pandas . tools . plotting import _plot_klass <EOL> plotf = _plot_klass [ plotf ] . _plot <EOL> lines . append ( plotf ( ax , series . index . _mpl_repr ( ) , <EOL> series . values , ** kwds ) [ <NUM_LIT:0> ] ) <EOL> labels . append ( pprint_thing ( series . name ) ) <EOL> return lines , labels <EOL> def _decorate_axes ( ax , freq , kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( ax , '<STR_LIT>' ) : <EOL> ax . _plot_data = [ ] <EOL> ax . freq = freq <EOL> xaxis = ax . get_xaxis ( ) <EOL> xaxis . freq = freq <EOL> if not hasattr ( ax , '<STR_LIT>' ) : <EOL> ax . legendlabels = [ kwargs . get ( '<STR_LIT:label>' , None ) ] <EOL> else : <EOL> ax . legendlabels . append ( kwargs . get ( '<STR_LIT:label>' , None ) ) <EOL> ax . view_interval = None <EOL> ax . date_axis_info = None <EOL> def _get_freq ( ax , series ) : <EOL> freq = getattr ( series . index , '<STR_LIT>' , None ) <EOL> if freq is None : <EOL> freq = getattr ( series . index , '<STR_LIT>' , None ) <EOL> ax_freq = getattr ( ax , '<STR_LIT>' , None ) <EOL> if ax_freq is None : <EOL> if hasattr ( ax , '<STR_LIT>' ) : <EOL> ax_freq = getattr ( ax . left_ax , '<STR_LIT>' , None ) <EOL> elif hasattr ( ax , '<STR_LIT>' ) : <EOL> ax_freq = getattr ( ax . right_ax , '<STR_LIT>' , None ) <EOL> if freq is None : <EOL> freq = ax_freq <EOL> if isinstance ( freq , DateOffset ) : <EOL> freq = freq . rule_code <EOL> else : <EOL> freq = frequencies . get_base_alias ( freq ) <EOL> freq = frequencies . get_period_alias ( freq ) <EOL> return freq , ax_freq <EOL> def _use_dynamic_x ( ax , data ) : <EOL> freq = _get_index_freq ( data ) <EOL> ax_freq = getattr ( ax , '<STR_LIT>' , None ) <EOL> if freq is None : <EOL> freq = ax_freq <EOL> else : <EOL> if ( ax_freq is None ) and ( len ( ax . get_lines ( ) ) > <NUM_LIT:0> ) : <EOL> return False <EOL> if freq is None : <EOL> return False <EOL> if isinstance ( freq , DateOffset ) : <EOL> freq = freq . rule_code <EOL> else : <EOL> freq = frequencies . get_base_alias ( freq ) <EOL> freq = frequencies . get_period_alias ( freq ) <EOL> if freq is None : <EOL> return False <EOL> if isinstance ( data . index , DatetimeIndex ) : <EOL> base = frequencies . get_freq ( freq ) <EOL> x = data . index <EOL> if ( base <= frequencies . FreqGroup . FR_DAY ) : <EOL> return x [ : <NUM_LIT:1> ] . is_normalized <EOL> return Period ( x [ <NUM_LIT:0> ] , freq ) . to_timestamp ( tz = x . tz ) == x [ <NUM_LIT:0> ] <EOL> return True <EOL> def _get_index_freq ( data ) : <EOL> freq = getattr ( data . index , '<STR_LIT>' , None ) <EOL> if freq is None : <EOL> freq = getattr ( data . index , '<STR_LIT>' , None ) <EOL> if freq == '<STR_LIT:B>' : <EOL> weekdays = np . unique ( data . index . dayofweek ) <EOL> if ( <NUM_LIT:5> in weekdays ) or ( <NUM_LIT:6> in weekdays ) : <EOL> freq = None <EOL> return freq <EOL> def _maybe_convert_index ( ax , data ) : <EOL> if isinstance ( data . index , DatetimeIndex ) : <EOL> freq = getattr ( data . index , '<STR_LIT>' , None ) <EOL> if freq is None : <EOL> freq = getattr ( data . index , '<STR_LIT>' , None ) <EOL> if isinstance ( freq , DateOffset ) : <EOL> freq = freq . rule_code <EOL> if freq is None : <EOL> freq = getattr ( ax , '<STR_LIT>' , None ) <EOL> if freq is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> freq = frequencies . get_base_alias ( freq ) <EOL> freq = frequencies . get_period_alias ( freq ) <EOL> data = data . to_period ( freq = freq ) <EOL> return data <EOL> def format_dateaxis ( subplot , freq ) : <EOL> """<STR_LIT>""" <EOL> majlocator = TimeSeries_DateLocator ( freq , dynamic_mode = True , <EOL> minor_locator = False , <EOL> plot_obj = subplot ) <EOL> minlocator = TimeSeries_DateLocator ( freq , dynamic_mode = True , <EOL> minor_locator = True , <EOL> plot_obj = subplot ) <EOL> subplot . xaxis . set_major_locator ( majlocator ) <EOL> subplot . xaxis . set_minor_locator ( minlocator ) <EOL> majformatter = TimeSeries_DateFormatter ( freq , dynamic_mode = True , <EOL> minor_locator = False , <EOL> plot_obj = subplot ) <EOL> minformatter = TimeSeries_DateFormatter ( freq , dynamic_mode = True , <EOL> minor_locator = True , <EOL> plot_obj = subplot ) <EOL> subplot . xaxis . set_major_formatter ( majformatter ) <EOL> subplot . xaxis . set_minor_formatter ( minformatter ) <EOL> subplot . format_coord = lambda t , y : ( <EOL> "<STR_LIT>" . format ( Period ( ordinal = int ( t ) , freq = freq ) , y ) ) <EOL> pylab . draw_if_interactive ( ) </s>
