extjs grid 动态设置只读_【实例分享】用pyecharts绘热点板块动态图

   dabd2fd3cf722d9ed3691aec035daf45.gif
 
      之前一直用matlab,后来由于调取交易数据过程中总会遇到各种不方便,去年尝试学习python开始用起来,实际用处最大就是汇总整理数据,再绘图展示。

      pyecharts真的好好用!!!做一个学习过程中的实例分享!0e70883c9489096b98f09fbe537b40b2.png

    具体应用在A股上追踪板块和热门股票。效果图如下~

b1a17d755867bd66ca3d350f8a26dece.gif

c3dda381caee68d9e8f2d6c3a0ea69d4.png

      最近都比较关注A股,其中板块和概念一直更是关注的重点,最常见的就是wind的板块跟踪,虽然今天绿油油,但是总想知道过去一段时间热点都在哪些板块和股票吧~

588333c8e1ebde236d29ee7fa1aae6a1.png

    操作环境   

  •    项目基于 python 3.7版本 / pyecharts 1.7.1 / tushare 1.2.54

  •    tushare 1.2.54

    板块定义:申万一级行业分类(共27项)

    板块内部权重分配:按照当日成交量加权

    数据来源:tushare

Pyecharts-时间轴动态图

       数据来源基于tushare的公开日线股票数据,详细见tushare官网数据调用方式:https://tushare.pro/document/2,如下分别获得申万的一级行业列表,及行业分类、股票行情。

13b8dd840fbf45ae7cd72841a4cb36fe.png

1b690148b3048a29b5f698228b270a51.png

d6360886cfc1102ed7fa2910f612d9c2.png

把三个所需要的信息拼凑在一张dataframe上,这里命名为:Industry_ALL,数据如下:

1b844c3bed6699fd74a2992fe2353eaa.png

我们先做一个没有时间轴的静态图,从 Industry_ALL 中提取出一周数据,并按照成交量从高到低排序:

Industry_week = Industry_ALL[Industry_ALL.trade_date.isin(open_date.tail(5).index.tolist())] #本周交易日为5个交易日,可修改#过去一周板块内容统计for k in Industry.index:    Industry.loc[k,'pct_chg'] = Industry_week[Industry_week.index_code.isin([Industry.index_code[k]])].pct_chg.sum() #周涨跌幅    Industry.loc[k,'amount'] = Industry_week[Industry_week.index_code.isin([Industry.index_code[k]])].amount.mean()  #本周日均成交金额Industry = Industry.sort_values(by='amount',ascending=False)

     导入pyecharts中所需要的类和函数

from pyecharts import options as optsfrom pyecharts.charts import Bar, Timeline,Line,Grid,Pagefrom pyecharts.commons.utils import JsCodefrom pyecharts.faker import Faker

   这里我们设置为有涨跌幅、日均成交量两边坐标轴的格式,并采用visualmap_opts()设置value对应的颜色变化

x = Industry.industry_name.tolist()bar =(    Bar()    .add_xaxis(x)    .add_yaxis("周涨跌幅", Industry.pct_chg.tolist())    #.add_yaxis("日均成交量", Industry.amount.tolist())    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))    .set_global_opts(         title_opts=opts.TitleOpts(title="申万一级行业_week".format(i),subtitle="(按当日成交量加权)".format(i),pos_left="10%"),                   visualmap_opts=opts.VisualMapOpts(is_show=False,type_='color',is_calculable=True, min_=-4,max_=4,orient="horizontal",pos_right="20%",split_number=300),        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=60,font_size=15)),                yaxis_opts=opts.AxisOpts(name="周涨跌幅",position="left",axislabel_opts=opts.LabelOpts(formatter="{value} %"),),                tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),          )    .extend_axis(        yaxis=opts.AxisOpts(            type_="value",            name="日均成交量",            position="right",            #axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts(color="#675bba") ),            axislabel_opts=opts.LabelOpts(formatter="{value}亿"),            splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=10)            ),        )    )    )    line = (    Line()    .add_xaxis(x)    .add_yaxis("日均成交量",Industry.amount.tolist(),        yaxis_index=1,    #删了一个Y轴,Y轴索引由2改为1        label_opts=opts.LabelOpts(is_show=False),         ))   bar.overlap(line)grid = Grid()grid.add(bar,opts.GridOpts(pos_left="5%", pos_right="10%"), is_control_axis_index=True)grid.render_notebook()

得到效果图如下:

f06064a7b1c55c482589d246d573669d.png

Timeline时间轴

     在上述静态图的基础上,我们添加timeline()函数,生成一个时间轴,相当于做一个循环,对应不同时间点静态图集合在时间轴上按照时间展示:

   tl = Timeline()for i in open_date.index:    ...       tl.add_schema(orient= "horizontal",play_interval=200,symbol="rect",symbol_size=[25,1],is_auto_play=True,pos_bottom="-25px")        tl.add(grid, "{}".format(i))

其中add_schema()可以设置时间轴的大小位置、播放参数等(具体代码见文末)。

b1a17d755867bd66ca3d350f8a26dece.gif

最终可以得到一个自动循环播放的动态图,清楚展示过去30个交易日中,申万一级行业分类中,大家的热点基本都保持在化工、有色、医药生物、电子、计算机、非银金融等几个板块。

龙虎榜-词云图

     从tushare中读取最近30个交易日的龙虎榜股票数据,剔除其中的退市和ST股票,得到数据data:

dragon_stock = pd.DataFrame(columns=['trade_date', 'ts_code', 'name', 'close', 'pct_change', 'turnover_rate',       'amount', 'l_sell', 'l_buy', 'l_amount', 'net_amount', 'net_rate',       'amount_rate', 'float_values', 'reason'])for i in open_date.index:    dragon_stock_1 = pro.top_list(trade_date=i)    dragon_stock = pd.concat([dragon_stock,dragon_stock_1], axis=0)    dragon_stock = dragon_stock[~dragon_stock['name'].str.contains('退|ST')]  #剔除退市和ST的股票data = []dragon_dicts = dragon_stock['name'].value_counts().to_dict()for key in dragon_dicts:    data.append((key, dragon_dicts[key]))
from pyecharts.charts import WordCloud mywordcloud = WordCloud()mywordcloud.add('龙虎榜-词云图',data, shape='circle',width=800,height=450,is_draw_out_of_bound=False)### 渲染图片mywordcloud.render_notebook()

自带鼠标的标签功能,绘图后可以得到如下效果:

24c0b5210653b16d2b8eb8bb352f7c79.png

Pyecharts-时间轴动态图 完整代码:

x = Industry_ALL.drop_duplicates('industry_name').industry_name.tolist()tl = Timeline()for i in open_date.index:    bar = (        Bar()        .add_xaxis(x)       # .add_yaxis("涨跌幅", Industry_ALL[Industry_ALL.trade_date.isin([i])].pct_chg.tolist()) #添加纵轴变量1的values        .add_yaxis("日成交量",Industry_ALL[Industry_ALL.trade_date.isin([i])].amount.tolist()) #添加纵轴变量2的values        .set_series_opts(            label_opts=opts.LabelOpts(is_show=False),            markline_opts=opts.MarkLineOpts(            data=[ opts.MarkLineItem(  name="", y = 1000,symbol = None  )],            #linestyle_opts=opts.LineStyleOpts(width = 1.5,color = 'plum',)                    ),    )                .extend_axis(            yaxis=opts.AxisOpts(type_="value",name="涨跌幅",position="right",min_=-10,max_=10, interval=2,            #axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts(color="#675bba") ),            axislabel_opts=opts.LabelOpts(formatter="{value}%"),            splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=10)            ),        )    )               .set_global_opts(                        title_opts=opts.TitleOpts(title="申万一级行业涨跌幅".format(i),subtitle="(按当日成交量加权)".format(i),pos_left="15%"),                       visualmap_opts=opts.VisualMapOpts(is_show=False,type_='color',is_calculable=True, min_=0,max_=800,orient="horizontal",pos_right="20%",split_number=300),              xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=60,font_size=11)),            yaxis_opts=opts.AxisOpts(name="(单位:亿元)",position="left",min_=0,max_=2000, interval=200, axislabel_opts=opts.LabelOpts(formatter="{value}"),),                    )    )    line = (    Line()    .add_xaxis(x)    .add_yaxis("涨跌幅",Industry_ALL[Industry_ALL.trade_date.isin([i])].pct_chg.tolist(),        yaxis_index=1,    #删了一个Y轴,Y轴索引由2改为1        label_opts=opts.LabelOpts(is_show=False),          linestyle_opts=opts.LineStyleOpts(is_show= True,width = 1.5,color = 'plum',),      )           )    bar.overlap(line)    grid = Grid()    grid.add(bar,opts.GridOpts(pos_left="5%", pos_right="10%"), is_control_axis_index=True)    tl.add_schema(orient= "horizontal",play_interval=200,symbol="rect",symbol_size=[25,1],is_auto_play=True,pos_bottom="-25px")        tl.add(grid, "{}".format(i))tl.render_notebook()

小小分享,希望大家喜欢~

1343722b898b91403f9f508920b7b132.png796808790520f81a06f8b45705af3546.pngdd74f5fe83a0f3b97231b785a4b95bf6.png6fe180304637ee2274256f4a5aa7269a.png8b39e474ad6548bc0885766853e51a4e.png036038662ae8726828a744442e8358b0.png80e3da0b36e30017de2df68b9ca2c75e.png1fa3c8822d00918f69e732c3a639e471.png164170c054655e7e16cce4ef0ab52b2e.png182e7a44262e579a4a25e4c17049f355.png


a7bbcd6c0b394100d5cff3e056dc3bd4.png

微信扫一扫

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值