K Line Chart
python实现k线图的代码,之前找过matplotlib中文文档但是画k线图的finance方法已经弃用了。所以自己在网上搜寻一下加上改编,很好的实现出k线图,
代码如下:__main__
# conding:utf-8 # 导入聚宽函数库 from jqdatasdk import * import pandas as pd import matplotlib.pyplot as plt from KLineChart.mpl_finance import plt_KLineChart import os ''' :param fields 字符串list, 默认是None(表示['open', 'close', 'high', 'low', 'volume', 'money']这几个标准字段), 支持以下属性 ['open', 'close', 'low', 'high', 'volume', 'money', 'factor', 'high_limit', 'low_limit', 'avg', 'pre_close', 'paused'] :param skip_paused 是否跳过不交易日期 ''' auth('your ID','your password') fields = ['open', 'close', 'high', 'low', 'volume', 'money'] stock_code = ['600519.XSHG','000001.XSHE','IC9999.CCFX'] data = get_price(stock_code,start_date='2018-1-1',end_date='2018-8-29',frequency='1d',fields=fields,skip_paused=False) k,m,n = data.shape # print(k,m,n) for i in range(n): Data = data.iloc[:,:,i] plt_KLineChart(Data,stock_code[i],step=20,fontSize=14) plt.show() # 数据保存 # os.mkdir('data/中国银行.xlsx') # data.to_excel(r'./data/中国银行.xlsx') # sql.to_excel() # print(data.shape)
这下我们需要导入自定义函数:from mpl_finance import plt_KLineChart
""" A collection of functions for analyzing and plotting financial data. User contributions welcome! """ from __future__ import (absolute_import, division, print_function, unicode_literals) import numpy as np import matplotlib.pyplot as plt from matplotlib import colors as mcolors from matplotlib.collections import LineCollection, PolyCollection from matplotlib.lines import TICKLEFT, TICKRIGHT, Line2D from matplotlib.patches import Rectangle from matplotlib.transforms import Affine2D from six.moves import xrange, zip def plot_day_summary_oclh(ax, quotes, ticksize=3, colorup='k', colordown='r'): """Plots day summary Represent the time, open, close, high, low as a vertical line ranging from low to high. The left tick is the open and the right tick is the close. Parameters ---------- ax : `Axes` an `Axes` instance to plot to quotes : sequence of (time, open, close, high, low, ...) sequences data to plot. time must be in float date format - see date2num ticksize : int open/close tick marker in points colorup : color the color of the lines where close >= open colordown : color the color of the lines where close < open Returns ------- lines : list list of tuples of the lines added (one tuple per quote) """ return _plot_day_summary(ax, quotes, ticksize=ticksize, colorup=colorup, colordown=colordown, ochl=True) def plot_day_summary_ohlc(ax, quotes, ticksize=3, colorup='k', colordown='r'): """Plots day summary Represent the time, open, high, low, close as a vertical line ranging from low to high. The left tick is the open and the right tick is the close. Parameters ---------- ax : `Axes` an `Axes` instance to plot to quotes : sequence of (time, open, high, low, close, ...) sequences data to plot. time must be in float date format - see date2num ticksize : int open/close tick marker in points colorup : color the color of the lines where close >= open colordown : color the color of the lines where close < open Returns ------- lines : list list of tuples of the lines added (one tuple per quote) """ return _plot_day_summary(ax, quotes, ticksize=ticksize, colorup=colorup, colordown=colordown, ochl=False) def _plot_day_summary(ax, quotes, ticksize=3, colorup='k', colordown='r', ochl=True): """Plots day summary Represent the time, open, high, low, close as a vertical line ranging from low to high. The left tick is the open and the right tick is the close. Parameters ---------- ax : `Axes` an `Axes` instance to plot to quotes : sequence of quote sequences data to plot. time must be in float date format - see date2num (time, open, high, low, close, ...) vs (time, open, close, high, low, ...) set by `ochl` ticksize : int open/close tick marker in points colorup : color the color of the lines where close