欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python : pyecharts 1.1.0 画K线图

程序员文章站 2022-03-18 23:46:35
...
pip install pyecharts ; 会安装 pyecharts-1.1.0
画K线图 kline1.py
# coding: utf-8
import os, sys
from pyecharts import options as opts
from pyecharts.charts import Kline

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

if len(code) !=6:
	print(' code length is 6')
	sys.exit(2)

file1 = code +'.csv'
file2 = code +'_1.html'

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

# 数据项: [open,close,low,high,volume]
#  (即:[开盘值,收盘值,最低值,最高值,成交量])
vlist = []
xlist = []
ylist = []
fp = open(file1,'r')
fp.readline() # 跳过第1行表头: ,date,open,close,high,low,volume,code
for line in fp:
	idx,date,open,close,high,low,volume,code = line.strip().split(',')
	xlist.append(date)
	vlist.append(volume)
	ylist.append([open,close,low,high])
print(str(len(xlist)))
kline = Kline()
kline.add_xaxis(xlist).add_yaxis("kline",ylist)
kline.set_global_opts(
            xaxis_opts=opts.AxisOpts(is_scale=True),
            yaxis_opts=opts.AxisOpts(
                is_scale=True,
                splitarea_opts=opts.SplitAreaOpts(
                    is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
                ),
            ),
            datazoom_opts=[opts.DataZoomOpts()],
            title_opts=opts.TitleOpts(title="日K线图:{0}".format(code)),
        )
#kline.set_opts(mark_line_valuedim="close")
kline.render(path=file2)

参考: pyecharts-1.1.0/example/kline_example.py
相关标签: python pyecharts