多彩色背景图

时间:2017-03-16 20:09:45

标签: python matplotlib

我有一个时间序列图表,希望显示各种"政权"按颜色在背景中。为了做到这一点,我想创建一个跨时间垂直多色背景的图表。

以下是两只股票的时间序列图:

import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.pylab as pylab
%matplotlib inline

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   = '2010-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()

我想要做的是指定背景的颜色。我想通过指定何时开始新颜色来执行此操作:

Beginning - blue
7/20/2014 - green
6/31/2016 - black
4/1/2016 and onward - grey

基于以上所述,我希望看到垂直颜色的图表从引用的开始日期开始变化,并指定颜色。

0 个答案:

没有答案