展现数据最好的方式,就是使用图表了,之前我使用最多的是matplotlib进行绘图,虽然非常的方便,但是无法进行交互,细节呈现并不是特别的完美,直到后期才发现了echart工具,使用echart来进行绘图简直完美。将数据从聚宽上拉取下来,使用pyecharts绘图,可使用鼠标进行拖拽与放大。pyecharts版本更新后api差别较大,本例是使用最新版pyecharts进行绘图,使用python必须是3.6以上才能支持。显示效果如下:
放大后效果:
代码如下:
import jqdatasdk as jq
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts import options as opts
from pyecharts.charts import Kline
from pyecharts.charts import Bar
from pyecharts.charts import Line
from pyecharts.globals import ThemeType
from example.commons import Faker
class StockDrawIndex(object):
def __init__(self):
self.default_save_name = "./cache/cache.html"
def _kline_splitarea(self, name, data, date):
c = (
Kline(init_opts=opts.InitOpts(width="1980px", height="720px"))
.add_xaxis(date)
.add_yaxis(
"kline",
data,
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="max", value_dim="close")]
),
)
.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)
),
),
title_opts=opts.TitleOpts(title=name),
datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")],
)
)
return c
def draw_kline(self, code="000001.XSHE", start_date='2005-01-01', stop_date=datetime.datetime.now().strftime("%Y-%m-%d")):
'''
输入参数:股票代码,绘制K线起始日期与结束日期,并标注最高点,可用滚轮调节区域
'''
df = jq.get_price(code)
df = df[(df.index >= start_date) & (df.index < = stop_date)]
global_stocks_df = self.sd.get_all_securities()
data = list()
date = list()
for i in range(len(df)):
_x = [df['open'][i], df['close'][i], df['low'][i], df['high'][i]]
data.append(_x)
date.append(str(df.index[i]).split(' ')[0])
name = code " " global_stocks_df.loc[code, "display_name"] " " global_stocks_df.loc[code, "name"]
return self._kline_splitarea(name, data, date)
def run(self):
self.draw_kline('000905.XSHG', start_date="2005-1-01").render(self.default_save_name)
StockDrawIndex().run()