熊猫:用基准图表绘制3个月的报价

时间:2016-06-18 17:07:12

标签: python pandas matplotlib

我有一个df与每日" Adj Close" (来自雅虎)对于我想要与SPY进行比较的许多股票。

            AAPL     ABC  ...    SPY
Date 
2016-03-10  100.56  85.79      198.52
2016-03-11  101.64  89.14      201.72
...         
2016-06-09  99.65   76.17      212.08
2016-06-10  98.83   76.46      210.07

我想描绘除SPY(基准)之外的每个股票的最近3个月。 每张图表都应该将股票价格与SPY价格进行比较。

这意味着,例如,对于绘制AAPL图表,基准SPY_AAPL应计算为:

SPY_AAPL = df['SPY'] / 198.52 * 100.56

...所以AAPL的第一点悬停在" SPY_AAPL"的那一点上。 绘制其他股票的图表需要类似的计算......

SPY_ABC = df['SPY'] / 198.52 * 85.79

我不确定必须将辅助列(SPY_AAPL,SPT_ABC ...)添加到df中。 Perpaps可以随时计算和#34;。

"图表系列"也许可以在" for循环"

for x in df.columns:    # how to remove the SPY here?
    df[x].tail(66).plot()    # 66 is approx 3 months
    # SPY_XXX.tail(66).plot()   # here the benchmark
    plt.show()

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您是否正在寻找这些方面的内容 - (将所有价格除以第一行):

stockData = DataReader(['AAPL', 'AMZN', 'FB', 'SPY'], 'yahoo', datetime(2015, 1, 1), datetime.today()).loc['Adj Close']
stockData = stockData.div(stockData.iloc[0], axis=1)
for stock in stockData.columns[:-1]:
    stockData.loc[:, [stock, 'SPY']].plot()
    plt.show()

enter image description here