用tushare极其简洁地下载股票数据后,可以用pyecharts强大的绘制功能,用几行代码即可画出具有丰富视觉效果的k线图。
最后使用freepy运行py代码的功能,快速展示K线图。
运行步骤:运行freepy——网络页签——获取列表——金融——tushare结合pyecharts生成股票K线图
# -*- coding: utf-8 -*-
"""
tushare结合pyecharts生成股票K线图
Created by freepy.
2022/1/24 23:27:43
"""
import tushare as ts
import os
from pyecharts import options as opts
from pyecharts.charts import Kline
stocks = '300038.SZ' ##中核科技
pro = ts.pro_api()
df = pro.daily(ts_code=stocks, start_date='20210701', end_date='20210731')
ochl = df[['open', 'close', 'high', 'low']]
ochl_tolist = [ochl.iloc[i].tolist() for i in range(len(ochl))]
c = (
Kline()
.add_xaxis(["2021/7/{}".format(i + 1) for i in range(31)])
.add_yaxis("", ochl_tolist)
.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=stocks),
)
.render("kline_datazoom_slider.html")
)
os.system(c)