Python 偏移索引图
我试图在一个指数图上画出两个股票价格。这个情节非常常见,因为它在同一地点以不同的价格开始两支股票 请参见下面的IBM与TSLA的对比图Python 偏移索引图,python,charts,timeserieschart,Python,Charts,Timeserieschart,我试图在一个指数图上画出两个股票价格。这个情节非常常见,因为它在同一地点以不同的价格开始两支股票 请参见下面的IBM与TSLA的对比图 def get_historical_closes(ticker, start_date, end_date): # get the data for the tickers. This will be a panel p = wb.DataReader(ticker, "yahoo", start_date, end_date) #
def get_historical_closes(ticker, start_date, end_date):
# get the data for the tickers. This will be a panel
p = wb.DataReader(ticker, "yahoo", start_date, end_date)
# convert the panel to a DataFrame and selection only Adj Close
# while making all index levels columns
d = p.to_frame()['Adj Close'].reset_index()
# rename the columns
d.rename(columns={'minor': 'Ticker', 'Adj Close': 'Close'}, inplace=True)
# pivot each ticker to a column
pivoted = d.pivot(index='Date', columns='Ticker')
# and drop the one level on the columns
pivoted.columns = pivoted.columns.droplevel(0)
return pivoted
tickers = ['IBM','TSLA']
start = '2015-12-31'
end ='2016-12-22'
df_ret=get_historical_closes(tickers,start,end).pct_change().replace('NaN',0)
df_ret=np.cumprod(1+df_ret)
df_ret.plot()
正如你所看到的,两者都从1点开始
我想做的是让1.00的收敛点位于日期索引中的任意点。例如,我希望看到相同的图表,除了2016年7月31日的直线收敛于1。因此,在给定点抵消指数收敛
有人知道如何做到这一点吗?试图让事情变得比实际情况更困难。见下文:
df_day=df_ret[df_ret.index=='2016-03-31']
df_plot = pd.DataFrame(index=df_ret.index, columns=df_ret.columns)
for col in df_ret.columns: # For each factor
df_plot[col]=df_ret[col]/df_day[col].values
df_plot.plot()