Python matplotlib中的排序和条件颜色格式
要跳过上下文直接进入问题,请转到“所需的更改” 我在下面编写了helper函数来Python matplotlib中的排序和条件颜色格式,python,pandas,matplotlib,Python,Pandas,Matplotlib,要跳过上下文直接进入问题,请转到“所需的更改” 我在下面编写了helper函数来 获取数据 计算年初至今的回报率 在条形图中绘制结果 以下是函数: def ytd_perf(symb, col_names, source = 'yahoo'): import datetime as datetime from datetime import date import pandas as pd import pandas_datareader.data as web
def ytd_perf(symb, col_names, source = 'yahoo'):
import datetime as datetime
from datetime import date
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import seaborn as sns
%pylab inline
#establish start and end dates
start = date(date.today().year, 1, 1)
end = datetime.date.today()
#fetch data
df = web.DataReader(symb, source, start = start, end = end)['Adj Close']
#make sure column orders don't change
df = df.reindex_axis(symb, 1)
#rename the columns
df.columns = col_names
#calc returns from the first element
df = (df / df.ix[0]) - 1
#Plot the most recent line of data -- this represents the YTD return
ax = df.ix[-1].plot(kind = 'bar', title = ('YTD Performance as of '+ str(end)),figsize=(12,9))
vals = ax.get_yticks()
ax.set_yticklabels(['{:3.1f}%'.format(x*100) for x in vals])
所以,当我跑步时:
tickers = ['SPY', 'TLT']
names = ['Stocks', 'Bonds']
ytd_perf(tickers, names)
我得到以下输出:
2我无法完全投入工作的期望变化: