使用 mpl_finance 库绘制K线图

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、mpl_finance 是什么?
  • 二、使用步骤
    • 1.数据准备
    • 2.绘制 K 线


前言

在使用 python 做量化金融时经常需要绘制 K 线。关于绘制 K 线的方式有很多种,接下来介绍一下使用 mpl_finance 绘制 K 线。


一、mpl_finance 是什么?

mpl_finance(matplotlib finance)是 python 中可以用来画出蜡烛图、线图的分析工具,目前已经从 matplotlib 中独立出来。

二、使用步骤

1.数据准备

600895.csv 文件数据:

Date,High,Low,Open,Close,Volume,Adj Close
2019-01-02,16.329999923706055,14.710000038146973,15.0600004196167,15.930000305175781,75979904,15.930000305175781
2019-01-03,16.649999618530273,15.3100004196167,15.779999732971191,16.239999771118164,94733382,16.239999771118164
2019-01-04,16.579999923706055,15.600000381469727,15.699999809265137,16.299999237060547,68985635,16.299999237060547
2019-01-07,16.649999618530273,15.600000381469727,15.699999809265137,16.290000915527344,59222671,16.290000915527344
2019-01-08,16.559999465942383,15.8100004196167,16.280000686645508,16.040000915527344,55522302,16.040000915527344
2019-01-09,16.329999923706055,15.75,16.020000457763672,16.020000457763672,52641127,16.020000457763672
2019-01-10,16.110000610351562,15.119999885559082,15.880000114440918,15.199999809265137,53202090,15.199999809265137
2019-01-11,15.800000190734863,15.0600004196167,15.199999809265137,15.5600004196167,42057493,15.5600004196167
2019-01-14,16.079999923706055,15.350000381469727,15.670000076293945,15.460000038146973,43255147,15.460000038146973
2019-01-15,15.640000343322754,15.09000015258789,15.399999618530273,15.539999961853027,31687291,15.539999961853027
2019-01-16,16.170000076293945,15.460000038146973,15.75,15.710000038146973,44711686,15.710000038146973
2019-01-17,17.049999237060547,15.600000381469727,15.600000381469727,16.979999542236328,86309543,16.979999542236328
2019-01-18,16.799999237060547,16.049999237060547,16.719999313354492,16.290000915527344,62198832,16.290000915527344
2019-01-21,16.530000686645508,15.920000076293945,16.219999313354492,16.399999618530273,38675827,16.399999618530273
2019-01-22,16.90999984741211,16.219999313354492,16.299999237060547,16.360000610351562,47087722,16.360000610351562
2019-01-23,16.68000030517578,15.90999984741211,16.360000610351562,16.399999618530273,40190374,16.399999618530273
2019-01-24,16.649999618530273,15.930000305175781,16.649999618530273,16.06999969482422,39457212,16.06999969482422
2019-01-25,16.040000915527344,15.270000457763672,15.920000076293945,15.329999923706055,42175769,15.329999923706055
2019-01-28,15.569999694824219,15.210000038146973,15.5,15.350000381469727,21769886,15.350000381469727
2019-01-29,15.5,14.180000305175781,15.270000457763672,14.539999961853027,31401261,14.539999961853027
2019-01-30,14.770000457763672,14.329999923706055,14.489999771118164,14.369999885559082,16274136,14.369999885559082
2019-01-31,14.75,13.579999923706055,14.6899995803833,13.800000190734863,32695437,13.800000190734863

2.绘制 K 线

代码如下(示例):

import pandas as pd
import matplotlib.pyplot as plt
from mpl_finance import candlestick2_ochl

# 从文件中获取数据
df = pd.read_csv('600895.csv', encoding='gbk', index_col=0)
# 设置图的位置
fig = plt.figure()
ax = fig.add_subplot(111)
# 调用方法绘制K线图
candlestick2_ochl(ax=ax,
                  opens=df["Open"].values,
                  closes=df["Close"].values,
                  highs=df["High"].values,
                  lows=df["Low"].values,
                  width=1,
                  colorup='red',
                  colordown='green')
# 设置x轴的标签
plt.xticks(range(len(df.index.values)), df.index.values, rotation=30)
ax.grid(True)  # 带网格线
plt.title("600895张江高科的K线图")
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.show()

你可能感兴趣的:(量化金融工具,python)