pyecharts 画K线图

中文版帮助文档 http://pyecharts.org/#/zh-cn/ Kline/Candlestick(K线图),
这里以画基金净值月K线图为例:

数据文件 M660008.csv

ym,open,close,low,high,mavg
201601,1.1141,0.9496,0.9215,1.1350,1.0223
201602,0.9356,0.9274,0.9274,1.0042,0.9666

... ...

201901,1.0194,1.0939,1.0177,1.0939,1.0652
201902,1.1085,1.2450,1.1085,1.2645,1.1812
201903,1.2711,1.3114,1.2414,1.3114,1.2801
201904,1.3447,1.3736,1.3440,1.3736,1.3557

画月K线图 kline_m.py

# coding=utf-8
import os, sys
from pyecharts import Kline
from pyecharts import Line
from pyecharts import Overlap

if len(sys.argv) ==2:
    fcode = sys.argv[1]
else:
    print('usage: kline_m.py fcode ')
    sys.exit(1)

if len(fcode) !=6:
    print(' fcode is char(6)')
    sys.exit(2)

file1 = 'M'+fcode +'.csv'
file2 = 'M'+fcode +'.html'

if not os.path.exists(file1):
    print(file1 +' is not exists.')
    sys.exit(3)

# 数据项: [open,close,low,high,mavg] 
#  (即:[开盘值,收盘值,最低值,最高值,平均值])
alist = []
clist = []
vlist = []
fp = open(file1,'r')
fp.readline() # 跳过第1行表头: ym,open,close,low,high,mavg
for line in fp:
    ym,open,close,low,high,mavg = line.strip().split(',')
    alist.append(ym)
    clist.append(mavg)
    vlist.append([open,close,low,high,mavg])
print(str(len(alist)))
title = fcode+" 基金净值 月K线图"
kline = Kline(title)
kline.add(fcode, alist, vlist, mark_line_valuedim="close")
#kline.render(path=file2)
# 平均线,还可以加多条线
lines = Line()
lines.add('mavg', alist, clist)
# 叠加的图
overlap = Overlap()
overlap.add(kline)
overlap.add(lines)
overlap.render(path=file2)

运行 kline_m.py 660008

pyecharts 画K线图_第1张图片

参考 https://blog.csdn.net/qtlyx/article/details/85221503

 

你可能感兴趣的:(python)