【宽客学院】数据可视化


(iQuant) #1

介绍了平台数据接口DataSource后,本文介绍如何实现常用的绘图功能。

为了方便大家绘图,平台封装了HighChart绘图工具T.plot,同时平台也支持常用的matlibplot模块和seaborn模块。

一、绘图模块T.plot

针对Pandas的DataFrame对象可以使用T.plot函数绘图, 在用户模块-共享模块中,我们可以找到T.plot绘图模块,可以拖拽模块至画布,连接m5模块和T.plot模块

模块参数解读
  • 指定x轴列名
  • 指定y轴列名:可以指定单列,或指定多列以逗号分隔
  • 指定图名
  • 指定类型:模块支持的绘图类型见下表
  • 自定义选项设置:设置图幅大小等内容。
选项 类别
candlestick K线图
scatter 散点图
spline 曲线图
line 折线图
area 面积图
areaspline 曲线区域图
areasplinerange 区间区域图
bar 条形图
bubble 气泡图
column 柱状图
columnrange 区间柱状图
errorbar 误差图
gauge 仪表图
pie 饼图
waterfall 瀑布图

二、案例解读

如下图,构建一个可视化流程:

  • 首先我们通过数据源模块m2获取历史日线行情数据(默认为后复权价格)。
  • 然后在特征列表模块m7中使用表达式引擎计算真实价格,并通过衍生特征抽取模块m6计算表达式。
  • 由于T.plot绘制K线图时默认识别open,close,low,high作为开盘价、收盘价、最低价和最高价,因此我们需要通过修改列名模块m5将计算的真实价格列名对应修改为open,close,low,high
  • 通过m10模块和m4模块分别绘制开盘价折线图和K线图

例1:以时间列date为x轴,开盘价open列为y值绘制开盘价的折线图

折线图设置

结果如下:

例2:绘制K线图

K线图设置

结果如下:

三、高级功能

如果想使用T.plot命令实现分栏显示、轴控制等高级功能可以参看下文

T.plot代码案例链接

附:本文绘图功能案例:

克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-162:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-162:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-169:input_data","SourceOutputPortId":"-162:data"},{"DestinationInputPortId":"-40:input_ds","SourceOutputPortId":"-169:data"},{"DestinationInputPortId":"-169:features","SourceOutputPortId":"-177:data"},{"DestinationInputPortId":"-59:input_ds","SourceOutputPortId":"-40:data"},{"DestinationInputPortId":"-117:input_ds","SourceOutputPortId":"-40:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2015-12-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000001.SZA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nlow\nhigh\nclose\nopen\nadjust_factor\namount","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-162","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-162"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-162"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-162","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-169","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-169"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-169"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-169","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-177","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\namount\nopen/adjust_factor\nclose/adjust_factor\nhigh/adjust_factor\nlow/adjust_factor","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-177"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-177","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"Comment":"","CommentCollapsed":true},{"Id":"-40","ModuleId":"BigQuantSpace.rename_columns.rename_columns-v5","ModuleParameters":[{"Name":"columns","Value":"open/adjust_factor:open|close/adjust_factor:close|high/adjust_factor:high|low/adjust_factor:low","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"keep_old_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-40"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-40","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-59","ModuleId":"BigQuantSpace.Tplot.Tplot-v1","ModuleParameters":[{"Name":"x","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"y","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"title","Value":"开盘价折线图","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"chart_type","Value":"spline","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"chart_options","Value":"{'chart':{'height': 500}}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-59"}],"OutputPortsInternal":[],"UsePreviousResults":true,"moduleIdForCode":10,"Comment":"绘制开盘价折线图","CommentCollapsed":false},{"Id":"-117","ModuleId":"BigQuantSpace.Tplot.Tplot-v1","ModuleParameters":[{"Name":"x","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"y","Value":"open,high,low,close,amount","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"title","Value":"K线图","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"chart_type","Value":"candlestick","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"chart_options","Value":"{'series': [{},{'type': 'column'}]}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-117"}],"OutputPortsInternal":[],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"绘制K线图","CommentCollapsed":false}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='211,64,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='556,68,200,200'/><NodePosition Node='-162' Position='371,178,200,200'/><NodePosition Node='-169' Position='447,277,200,200'/><NodePosition Node='-177' Position='675,175,200,200'/><NodePosition Node='-40' Position='455,357,200,200'/><NodePosition Node='-59' Position='172.43206787109375,469.0449523925781,200,200'/><NodePosition Node='-117' Position='516.7000122070312,469.035400390625,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [6]:
    # 本代码由可视化策略环境自动生成 2019年1月31日 14:49
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.instruments.v2(
        start_date='2015-01-01',
        end_date='2015-12-01',
        market='CN_STOCK_A',
        instrument_list='000001.SZA',
        max_count=0
    )
    
    m3 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    low
    high
    close
    open
    adjust_factor
    amount"""
    )
    
    m2 = M.use_datasource.v1(
        instruments=m1.data,
        features=m3.data,
        datasource_id='bar1d_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m7 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    amount
    open/adjust_factor
    close/adjust_factor
    high/adjust_factor
    low/adjust_factor"""
    )
    
    m6 = M.derived_feature_extractor.v3(
        input_data=m2.data,
        features=m7.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=True,
        user_functions={}
    )
    
    m5 = M.rename_columns.v5(
        input_ds=m6.data,
        columns='open/adjust_factor:open|close/adjust_factor:close|high/adjust_factor:high|low/adjust_factor:low',
        keep_old_columns=False
    )
    
    m10 = M.Tplot.v1(
        input_ds=m5.data,
        x='date',
        y='open',
        title='开盘价折线图',
        chart_type='spline',
        chart_options={'chart':{'height': 500}}
    )
    
    m4 = M.Tplot.v1(
        input_ds=m5.data,
        x='date',
        y='open,high,low,close,amount',
        title='K线图',
        chart_type='candlestick',
        chart_options={'series': [{},{'type': 'column'}]}
    )
    
    [2019-01-31 13:40:50.024151] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-31 13:40:50.029349] INFO: bigquant: 命中缓存
    [2019-01-31 13:40:50.030362] INFO: bigquant: instruments.v2 运行完成[0.006255s].
    [2019-01-31 13:40:50.034420] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-31 13:40:50.038951] INFO: bigquant: 命中缓存
    [2019-01-31 13:40:50.039848] INFO: bigquant: input_features.v1 运行完成[0.005453s].
    [2019-01-31 13:40:50.046996] INFO: bigquant: use_datasource.v1 开始运行..
    [2019-01-31 13:40:50.053149] INFO: bigquant: 命中缓存
    [2019-01-31 13:40:50.054260] INFO: bigquant: use_datasource.v1 运行完成[0.007272s].
    [2019-01-31 13:40:50.057194] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-31 13:40:50.062633] INFO: bigquant: 命中缓存
    [2019-01-31 13:40:50.063664] INFO: bigquant: input_features.v1 运行完成[0.006473s].
    [2019-01-31 13:40:50.107448] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-31 13:40:50.113660] INFO: bigquant: 命中缓存
    [2019-01-31 13:40:50.114664] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.007271s].
    [2019-01-31 13:40:50.118041] INFO: bigquant: rename_columns.v5 开始运行..
    [2019-01-31 13:40:50.123295] INFO: bigquant: 命中缓存
    [2019-01-31 13:40:50.124446] INFO: bigquant: rename_columns.v5 运行完成[0.006402s].
    [2019-01-31 13:40:50.128129] INFO: bigquant: Tplot.v1 开始运行..
    [2019-01-31 13:40:50.144940] INFO: bigquant: 命中缓存
    
    [2019-01-31 13:40:50.187813] INFO: bigquant: Tplot.v1 运行完成[0.059642s].
    [2019-01-31 13:40:50.191295] INFO: bigquant: Tplot.v1 开始运行..
    
    [2019-01-31 13:40:50.736915] INFO: bigquant: Tplot.v1 运行完成[0.545587s].
    

    本文介绍了T.plot模块的使用,对于热力图和分布图等统计图表可以使用Seaborn库实现。


    (zykphzx) #2

    小白,模块的参数设置可以介绍的详细点吗?


    (iQuant) #3

    您是哪里不太清楚呢,具体参数可以参考文档板块哈:https://bigquant.com/docs/faq/about_bigquant.html


    (zykphzx) #4

    模块的具体参数怎么设置,我去看一下


    (xuan) #5

    这样可以画多个技术指标

    def plot(ins,start_date,end_date,signal):
    
        # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
        def m5_run_bigquant_run(input_1, input_2, input_3):
            # 示例代码如下。在这里编写您的代码
            df = input_1.read()
            df=df.rename_axis({'open_0':'open','close_0':'close',"low_0":'low','high_0':'high'},axis='columns').set_index('date')
            ins=df.instrument.iloc[0]
            del df['instrument']    
            data_1 = DataSource.write_df(df)       
            return Outputs(data_1=data_1, data_2=ins, data_3=None)
    
        # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
        def m5_post_run_bigquant_run(outputs):
            return outputs
    
    
        m1 = M.instruments.v2(
            start_date=start_date,
            end_date=end_date,
            market='CN_STOCK_A',
            instrument_list=[ins],
            max_count=0
        )
    
        m3 = M.input_features.v1(
            features="""
        # #号开始的表示注释,注释需单独一行
        # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
        high_0
        close_0
        low_0
        open_0
        ta_sma_30_0   
        """+signal
            
        )
    
        m2 = M.general_feature_extractor.v7(
            instruments=m1.data,
            features=m3.data,
            start_date='',
            end_date='',
            before_start_days=90
        )
    
        m6 = M.derived_feature_extractor.v3(
            input_data=m2.data,
            features=m3.data,
            date_col='date',
            instrument_col='instrument',
            drop_na=False,
            remove_extra_columns=False,
            user_functions={}
        )
    
        m5 = M.cached.v3(
            input_1=m6.data,
            run=m5_run_bigquant_run,
            post_run=m5_post_run_bigquant_run,
            input_ports='',
            params='{}',
            output_ports=''
        )
    
        df=m5.data_1.read()
        ins=m5.data_2
        df=df[df.index>=start_date]
        select=['open', 'high', 'low', 'close','ta_sma_30_0']
        for i in signal.strip().split('\n'):
            select.append(i)        
        
        T.plot(df[select],
               # 设置图表title和高度;'series': [{},{'type': 'column'}] 设置第二个数据系列(即 amount)显示类型为柱状图(column)
               options={'chart': {'title': ins, 'height': 800}, 'series': [{},{'type': 'line','yAxis':0}]},
                   stock=True, candlestick=True)
        
    signal='''
    ta_bbands_upperband_14_0
    ta_bbands_middleband_14_0
    ta_bbands_lowerband_14_0
    close_0/1
    '''
    today_date ='2014-01-17'
    start_date= (pd.to_datetime(today_date) - datetime.timedelta(days=500)).strftime('%Y-%m-%d') 
    end_date= (pd.to_datetime(today_date) + datetime.timedelta(days=500)).strftime('%Y-%m-%d') 
    plot('000001.SZA',start_date,end_date,signal)