图有两部分,上面是蜡烛图,下面是volume,
直接分享,请大家捧场。
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 19 21:55:48 2017
@author: Wilson J
"""
import tushare as ts
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
from matplotlib.pylab import date2num
import datetime
import numpy as np
import time
import seaborn as sns
sns.set()
start_date='2015-06-30'
end_date=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
#ticker='600361'
ticker = input(' 请输入股票代码: ')
print ('***Start Running***')
print ('***正在绘图,请耐心等待***')
#源数据列标题排序为Date/Open/High/Lo/Close/Volume(名称可以改变)
hist_data=ts.get_k_data(ticker,start_date,end_date)
del hist_data['code']
hist_data=hist_data.reset_index()
hist_data=hist_data.set_index(['date'])
del hist_data['index']
# 对tushare获取到的数据转换成candlestick_ohlc()方法可读取的格式
quotes = []
for dates,row in hist_data.iterrows():
# 将时间转换为数字
date_time = datetime.datetime.strptime(dates,'%Y-%m-%d')
timeline = date2num(date_time)
Open,Close,High,Low,Volume= row[:5]
datas = (timeline,Open,High,Low,Close,Volume)
quotes.append(datas)
quotes=np.array(quotes)
fig,(ax1,ax2)=plt.subplots(2,sharex=True,figsize=(8,6))
mpf.candlestick_ohlc(ax1,quotes,width=0.6,colorup='r',colordown='g')
#plt.plot(hist_data.index,ma5)
ax1.set_title('%s Daily' % ticker, fontsize=14)
ax1.set_ylabel('Index_level')
ax1.grid(True)
ax1.xaxis_date()
plt.bar(quotes[:,0]-0.25,quotes[:,5],width=0.5)
ax2.set_ylabel('Volume')
ax2.grid(True)
ax2.autoscale_view()
plt.setp(plt.gca().get_xticklabels(),rotation=30)
'''
save pic
path=‘d:/’
from matplotlib.pyplot import savefig
savefig(path+stock_code+'Quotes'.jpg')
'''