# [python]使用python实现蜡烛图绘制

1.载入需要的模块
import tushare as ts

import matplotlib.pyplot as plt

import numpy as np

%matplotlib inline

%config InlineBackend.figure_format = 'retina'

%pylab inline

pylab.rcParams['figure.figsize'] = (10, 6) #设置绘图尺寸

from matplotlib.finance import candlestick_ohlc

from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY

2.绘图函数导入

def pandas_candlestick_ohlc(stock_data, otherseries=None):

# 设置绘图参数，主要是坐标轴

mondays = WeekdayLocator(MONDAY)

alldays = DayLocator()

dayFormatter = DateFormatter('%d')

fig, ax = plt.subplots()

fig.subplots_adjust(bottom=0.2)

if stock_data.index[-1] - stock_data.index[0] < pd.Timedelta('730 days'):

weekFormatter = DateFormatter('%b %d')

ax.xaxis.set_major_locator(mondays)

ax.xaxis.set_minor_locator(alldays)

else:

weekFormatter = DateFormatter('%b %d, %Y')

ax.xaxis.set_major_formatter(weekFormatter)

ax.grid(True)

# 创建K线图

stock_array = np.array(stock_data.reset_index()[['date','open','high','low','close']])

stock_array[:,0] = date2num(stock_array[:,0])

candlestick_ohlc(ax, stock_array, colorup = "red", colordown="green", width=0.4)

# 可同时绘制其他折线图

if otherseries is not None:

for each in otherseries:

plt.plot(stock_data[each], label=each)

plt.legend()

ax.xaxis_date()

ax.autoscale_view()

plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

plt.show()

3.载入数据，并运行函数

df = ts.get_hist_data('600848')

pandas_candlestick_ohlc(df)

4.绘图完成

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

http://blog.csdn.net/qq_36852770 Ash_Zhang的技术小屋；记录IT及代码的学习，以及极客生活

http://blog.sina.com.cn/happinessthinker ：幸福司马缸；记录个人管理 生活感悟