Python ggplot问题绘图> 8股票和传奇是截止的

时间:2014-03-20 19:58:35

标签: python matplotlib time-series stocks python-ggplot

首先让我说Python的ggplot是开发人员投入工作的开端。目前我在同一情节中遇到两个主要问题。如果我绘制8个或更少的股票,图像看起来不错,除了图例从图形区域运行(问题1)。如果我绘制超过8种股票,该情节会触发一些显然不能代表数据的不稳定线。此外,图例不会调整大小,而是留下额外的股票代码(问题2)。任何帮助表示赞赏。谢谢!

体面的地块代码:

import datetime
from ggplot import *
import pandas.io.data as web
import pandas as pd
import numpy as np

start = datetime.datetime(2014,1,1)
end = datetime.datetime(2014, 3,19)

stocks = ['APO','AVG','FI','ANIK','CELG','PACW','CBOE','BIIB']

stockData = {}
for ticker in stocks:
    stockData[ticker] = web.get_data_yahoo(ticker, start, end)

price = pd.DataFrame({tic: data['Adj Close'] for tic, data in stockData.iteritems()})

returns = price.pct_change()
returns = returns.apply(cumsum)

rt = returns.index
returns['Date'] = rt


# plotting the cum performance for each security

ret = pd.melt(returns, id_vars='Date') 
plot = ggplot(aes(x='Date', y='value', color='variable'),data=ret) +geom_line()

# plotting the equity curve of the theoretical portfolio

zt = returns
del zt['Date']
zt = zt.apply(np.sum, axis=1)
z = pd.DataFrame(zt, index=zt.index)
z['Date'] = rt
z.columns = ['equity curve', 'Date']


ret2 = pd.melt(z, id_vars='Date') 
plot2 = ggplot(aes(x='Date', y='value'),data=ret2) +geom_line()

print plot
print plot2

BAD Plot Code:

import datetime
from ggplot import *
import pandas.io.data as web
import pandas as pd
import numpy as np

start = datetime.datetime(2014,1,1)
end = datetime.datetime(2014, 3,19)

stocks = ['APO','AVG','FI','ANIK','CELG','PACW','CBOE','BIIB','ISIS', 'SDRL'] # <-- notice two additional tickers

stockData = {}
for ticker in stocks:
    stockData[ticker] = web.get_data_yahoo(ticker, start, end)

price = pd.DataFrame({tic: data['Adj Close'] for tic, data in stockData.iteritems()})

returns = price.pct_change()
returns = returns.apply(cumsum)

rt = returns.index
returns['Date'] = rt


# plotting the cum performance for each security

ret = pd.melt(returns, id_vars='Date') 
plot = ggplot(aes(x='Date', y='value', color='variable'),data=ret) +geom_line()

# plotting the equity curve of the theoretical portfolio

zt = returns
del zt['Date']
zt = zt.apply(np.sum, axis=1)
z = pd.DataFrame(zt, index=zt.index)
z['Date'] = rt
z.columns = ['equity curve', 'Date']


ret2 = pd.melt(z, id_vars='Date') 
plot2 = ggplot(aes(x='Date', y='value'),data=ret2) +geom_line()

print plot
print plot2

1 个答案:

答案 0 :(得分:2)

对于问题2,这是因为ggplot用完了颜色,您可以添加更多颜色来解决问题,只需将以下代码添加到代码的开头:

import ggplot as gg
gg.colors.COLORS.extend(["#ff0000", "#00ff00", "#0000ff"]) 

对于问题1,似乎我们需要在创建图形之后放置图例:

ret = pd.melt(returns, id_vars='Date').dropna()
plot = ggplot(aes(x='Date', y='value', color='variable'), data=ret) +geom_line()
fig = plot.draw()
ax = fig.axes[0]
offbox = ax.artists[0]
offbox.set_bbox_to_anchor((1, 0.5), ax.transAxes)

结果如下:

enter image description here