pip install pyecharts==1.9.1
pyecharts-1.9.1-py3-none-any.whl
我想在本地(PC) 画出 基金净值 月K线图,不想每次看图都需联网。
cd my_dir
mkdir echarts
cd echarts
curl -O https://assets.pyecharts.org/assets/echarts.min.js
修改一下开源代码
cd \Python38\Lib\site-packages\pyecharts\ 编辑 globals.py 第125行
class _OnlineHost:
DEFAULT_HOST = "https://assets.pyecharts.org/assets/"
改为 DEFAULT_HOST = "./echarts/"
编写 kline_m.py 如下
# coding=utf-8
import os, sys
from pyecharts import options as opts
from pyecharts.charts import Kline, Lineif len(sys.argv) ==2:fcode = sys.argv[1]file1 = 'M'+fcode +'.csv'file2 = 'M'+fcode +'.html'
elif len(sys.argv) ==3:fcode = sys.argv[1]file1 = 'M'+fcode +'_'+sys.argv[2]+'.csv'file2 = 'M'+fcode +'_'+sys.argv[2]+'.html'
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)# 定义K线图的数据:['open','close','low','high']
# 数据项: [open,close,low,high,mavg]
# (即:[开盘值,收盘值,最低值,最高值,平均值])
xlist = []
ylist = []
clist = []
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(',')xlist.append(ym)ylist.append([open,close,low,high])clist.append(mavg)
print(str(len(xlist)))
if len(sys.argv) ==2:title = fcode+" 基金净值 月K线图"
elif len(sys.argv) ==3:title = fcode+" 累计净值 月K线图"# 创建一个K线图实例
kline = Kline(init_opts=opts.InitOpts(width="1200px", height="600px"))# 设置x轴的数据
kline.add_xaxis(xlist)# 设置y轴的数据,这里的 "kline" 是系列名称,data 是K线图的数据
kline.add_yaxis("kline", ylist)# 设置全局配置项,包括x轴、y轴、标题等配置
kline.set_global_opts(xaxis_opts=opts.AxisOpts(is_scale=True), # 设置x轴的刻度是否自适应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=title), # 设置标题
)
# 平均线,还可以加多条线
line1 = Line().add_xaxis(xlist).add_yaxis('mavg', clist, yaxis_index=0)
line1.set_series_opts(label_opts=opts.LabelOpts(is_show=False),linestyle_opts=opts.LineStyleOpts(width=2),)
kline.overlap(line1)kline.render(path=file2)
运行 python kline_m.py 660008
生成 M660008.html
参考:pyecharts绘制K线图,并添加辅助线