如何在不将我的所有pandas数据转换为matplotlib和numpy的情况下,使标题名称显示为“AAPL Stock Price”。
import time
from pylab import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas.io.data import *
def Datapull(Stock):
try:
OHLC=DataReader(Stock,'yahoo',start='01/01/2010')
OHLC['diff'] = OHLC.Open - OHLC.Close
return OHLC
print 'Retrieved', Stock
time.sleep(5)
except Exception, e:
print 'Main Loop', str(e)
def graph(stock):
try:
fig=plt.figure()
mainplt=plt.subplot2grid((5,4), (0,0), rowspan=4, colspan=4)
stock['Close'].plot(color='g').set_ylabel('Price')
plt.title('Stock Price')
plt.setp(mainplt.get_xticklabels(), visible=False)
ax2=plt.subplot2grid((5,4), (4,0), rowspan=1, sharex=mainplt, colspan=4)
ax2.grid(False)
stock['Volume'].plot(color='c').set_ylabel('Volume')
ax2.axes.yaxis.set_ticklabels([])
plt.setp(ax2.get_xticklabels(), rotation=45)
plt.subplots_adjust(top=0.95, bottom=.14, right=.94, left=.09, wspace=.20, hspace=0 )
plt.show()
except Exception, e:
print 'Main Loop', str(e)
Stock='AAPL'
AAPL=Datapull(Stock)
graph(AAPL)
应该是
plt.title(stock+'Stock Price')
使用numpy但是我收到错误消息,而且suptitle也不起作用
Main Loop Could not operate ['Stock Price'] with block values [unsupported operand type(s) for +: 'numpy.ndarray' and 'str']
答案 0 :(得分:2)
加载数据的方式,不保存符号名称。 OHLC只是一个DataFrame,据我所知,它没有适当的名称属性,可以存储AAPL
符号。
一种可能的方法是加载Panel
,注意方括号,将stock_name转换为符号列表:
>>> stock_name = 'AAPL'
>>> OHLC = pd.io.data.DataReader([stock_name],'yahoo',start='01/01/2010')
>>> OHLC
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 990 (major_axis) x 1 (minor_axis)
Items axis: Open to Adj Close
Major_axis axis: 2010-01-04 00:00:00 to 2013-12-06 00:00:00
Minor_axis axis: AAPL to AAPL
>>> OHLC.axes[2]
Index([u'AAPL'], dtype=object)
并使用
访问基础DataFrame>>> OHLC.ix[:,:,'AAPL']
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 990 entries, 2010-01-04 00:00:00 to 2013-12-06 00:00:00
Data columns (total 6 columns):
Open 990 non-null values
High 990 non-null values
Low 990 non-null values
Close 990 non-null values
Volume 990 non-null values
Adj Close 990 non-null values
dtypes: float64(6)
或者您可以自己存储符号名称,
>>> OHLC = (stock_name, pd.io.data.DataReader(stock_name,'yahoo',start='01/01/2010'))
并使用
访问df和name>>> name, df = OHLC
>>> name
'AAPL'