tushare继续学习

使用yahoo数据的画K线图例子:
#!/usr/bin/env python
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, WeekdayLocator,\
DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
date1 = (2015, 7, 1)
date2 = (2015, 8, 28)
mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
alldays = DayLocator()              # minor ticks on the days
weekFormatter = DateFormatter(‘%b %d’)  # e.g., Jan 12
dayFormatter = DateFormatter(‘%d’)      # e.g., 12

quotes = quotes_historical_yahoo_ohlc(‘600030.SS’, date1, date2)
print(quotes)
if len(quotes) == 0:
raise SystemExit

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
#ax.xaxis.set_minor_formatter(dayFormatter)

#plot_day_summary(ax, quotes, ticksize=3)
candlestick_ohlc(ax, quotes, width=0.6)

ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment=’right’)

plt.show()

 

由于机器从win7 32位升级到64位(使用ghost直接升),所以anaconda又重新安装了一下,tushare毫无疑问重新安装了一下。

!pip install tushare
Collecting tushare
Downloading tushare-0.3.6.tar.gz
Installing collected packages: tushare
Running setup.py install for tushare
Successfully installed tushare-0.3.6
You are using pip version 7.0.3, however version 7.1.2 is available.
You should consider upgrading via the ‘python -m pip install –upgrade pip’ command.

 

练习矩阵操作:

r=np.random.standard_normal((4,3))

r=np.random.standard_normal((4,3))

r+s

r+3

练习matplotlib

import matplotlib as mpl

import matplotlib.pyplot as plt

%matplotlib inline

不明白上面第三句什么意思

开始画图:

 

np.random.seed(1000)

y=np.random.standard_normal(20)

x=range(len(y))

plt.plot(x,y)
Out[47]: [<matplotlib.lines.Line2D at 0x84124b0>]

那个就这样就画了一个最简单的图了。

那么怎么画K线图呢?我们试一下:

o=ts.get_hist_data(‘600030′)

o
Out[57]:
open   high  close    low      volume  price_change  p_change  date
2012-09-03  10.15  10.64  10.52  10.11   636278.81          0.30      2.94

首先通过tushare,拿到开盘数据,

然后画图:

plt.plot(range(728),o[‘open’])
Out[61]: [<matplotlib.lines.Line2D at 0x878dbb0>]

o.index是索引

当然x轴可以省略

plt.plot(o[‘open’])
Out[77]: [<matplotlib.lines.Line2D at 0x992cef0>]

 

加上网格,并且布满:

plt.plot(o[‘open’]);plt.axis(‘tight’);plt.grid(True)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>