Tushare( http://tushare.org )是一个多种数据集成API,可以抓取股市、电影票房等多种公开来源的数据,使用pandas( http://pandas.pydata.org/ )的DadaFrame作为内存数据结构,可以使用Python的多种接口访问,以及通过matplot绘制图表。
安装Docker上的Jupyter Notebook
docker run --name notebook --restart always -it -p 8800:8888 jupyter/all-spark-notebook
注意:我们将端口改为8800,按照提示运行时注意修改端口。
安装Tushare
在浏览器打开jupyter notebook服务地址,创建一个终端窗口。
安装软件:
pip install lxml
pip install tushare
运行Tushare
打开一个python3的notebook窗口,输入下面的代码,按shift+enter即可运行。
import matplotlib
import tushare as ts
import pandas as pd
import matplotlib.pyplot as plt
df=ts.get_hist_data('300036',start='2016-01-01',end='2017-01-01')
绘制图形
首先使用pandas内置的函数绘图:
with pd.plot_params.use('x_compat', True):
df.high.plot(color='r',figsize=(10,4),grid='on')
df.low.plot(color='b',figsize=(10,4),grid='on')
fig.savefig('/Users/david/pythongraph/graph000001.png')
如果使用Matplot绘图,需要首先运行 %matplot inline,参考: