zoukankan      html  css  js  c++  java
  • Python使用Plotly绘图工具,绘制面积图

    今天我们来讲一下如何使用Python使用Plotly绘图工具,绘制面积图

    绘制面积图与绘制散点图和折线图的画法类似,使用plotly graph_objs

    中的Scatter函数,不同之处在于面积图对fill属性的设置

    也就是说,相当于是在折线图的基础上,对图形进行填充

    import plotly as py
    import plotly.graph_objs as go
    import numpy as np
    
    pyplt = py.offline.plot
    
    # 随机生成100个交易日的收益率
    s1 = np.random.RandomState(8) # 定义局部种子
    s2 = np.random.RandomState(9) # 定义局部种子
    rd1 = s1.rand(100)/10 - 0.02
    rd2 = s2.rand(100)/10 - 0.02
    
    # 设定初始资金
    initial1 = 100000
    initial2 = 100000
    total1 = []
    total2 = []
    for i in range(len(rd1)):
        initial1 = initial1*rd1[i] + initial1
        initial2 = initial2*rd2[i] + initial2
        total1.append(initial1)
        total2.append(initial2)
    
    trace1 = go.Scatter(
    #     x = [1, 2, 3, 4],
        y = total1,
        fill = 'tonexty',
        mode= 'none', # 无边界线
        name = "策略1"
    )
    trace2 = go.Scatter(
    #     x = [1, 2, 3, 4],
        y = total2,
        fill = 'tozeroy',
        mode= 'none',# 无边界线
        name = "策略2"
    )
    
    data = [trace1, trace2]
    
    layout = dict(title = '策略净值曲线',
                  xaxis = dict(title = '交易天数'),
                  yaxis = dict(title = '净值'),
                  )
    fig = dict(data = data, layout = layout)
    pyplt(fig, filename='tmp/1.html')

    运行如上代码,会得到如上图所示的图形面积图

    展示了两个不同的交易策略的净值曲线图,数据是随机生成的

    就相当于做了折线图后,进行了填充得到

    我们在来说一下内部填充面积图

    内部填充面积图是仅仅填充两条曲线交叉所形成的面积部分,同样设置fill属性来完成。

    只需要在原来的面积图上设置第一条曲线无填充效果即可

    下面我们来看看代码

    import plotly as py
    import plotly.graph_objs as go
    import numpy as np
    
    pyplt = py.offline.plot
    
    # 随机生成100个交易日的收益率
    s1 = np.random.RandomState(8) # 定义局部种子
    s2 = np.random.RandomState(9) # 定义局部种子
    rd1 = s1.rand(100)/10 - 0.02
    rd2 = s2.rand(100)/10 - 0.02
    
    # 设定初始资金
    initial1 = 100000
    initial2 = 100000
    total1 = []
    total2 = []
    for i in range(len(rd1)):
        initial1 = initial1*rd1[i] + initial1
        initial2 = initial2*rd2[i] + initial2
        total1.append(initial1)
        total2.append(initial2)
    
    trace1 = go.Scatter(
        y = total1,
        fill = None,
        mode= 'lines', # 无边界线
        name = "策略1"
    )
    trace2 = go.Scatter(
    #     x = [1, 2, 3, 4],
        y = total2,
        fill = 'tonexty',
        mode= 'lines',# 无边界线
        name = "策略2"
    )
    
    data = [trace1, trace2]
    
    layout = dict(title = '策略净值曲线',
                  xaxis = dict(title = '交易天数'),
                  yaxis = dict(title = '净值'),
                  )
    fig = dict(data = data, layout = layout)
    pyplt(fig, filename='tmp/1.html')

    运行上诉代码,我们可以得到如上图所示的内部填充面积图

    我们设置了fill = None,

    在设置第二条曲线的填充的效果为tonexty

    即fill = 'tonexty' 即可得到如上图所示的图例

    接下来我们讲解一下堆积面积图

    堆积面积图与之前我写的博客中,层叠柱状图类似

    都是展示了数据累加的效果

    不同之处在于对数据的设置

    import plotly as py
    import plotly.graph_objs as go
    data_1 = go.Scatter(
        x = ['基金1', '基金2', '基金3', '基金4','基金5'],
        y = [32.52, 43.12, 43.47, 44.36, 33.11],
        name = '股票投资',
        mode = 'lines',
        line = dict(width=0.5,
                  color = 'rgb(184, 247, 212)'),
        fill = 'tonexty'
    )
    
    data_2 = go.Scatter(
        x = ['基金1', '基金2', '基金3', '基金4','基金5'],
        y = [63.24, 54.33, 74.28, 63.91, 63.11],
        name = '其它投资',
        mode = 'lines',
        line = dict(width=0.5,
                  color = 'rgb(111, 231, 219)'),
        fill = 'tonexty'
    )
    
    data_3 = go.Scatter(
        x = ['基金1', '基金2', '基金3', '基金4','基金5'],
        y = [83.24, 74.33, 93.91, 79.22, 83.11],
        name='债券投资',
        mode='lines',
        line=dict(width=0.5,
                  color='rgb(127, 166, 238)'),
        fill='tonexty'
    )
    
    data_4 = go.Scatter(
        x = ['基金1', '基金2', '基金3', '基金4','基金5'],
        y = [100, 100, 100, 100, 100],
        name='银行存款',
        mode='lines',
        line=dict(width=0.5,
                  color='rgb(131, 90, 241)'),
        fill='tonexty'
    )
    
    data = [data_1, data_2, data_3, data_4]
    
    
    layout = go.Layout(
        title = '基金资产配置比例图',
        showlegend = True,
        xaxis = dict(
            type = 'category',
        ),
        yaxis = dict(
            type = 'linear',
            range = [1, 100],
            dtick = 20,
            ticksuffix = '%'
        )
    )
    
    pyplt = py.offline.plot
    fig = go.Figure(data = data, layout = layout)
    pyplt(fig, filename = 'tmp/stacked-area-plot.html')

    可以看到,运行上诉代码,可以得到如上图所示的堆积面积图图例

    注意的是,在绘制层叠柱状图时需要设置stack模式,而绘制堆积面积图时则不需要

    这就是堆积面积图需要累加数据的原因

    在本质上,堆积面积图的堆积效果是在同一个图形中绘制对个面积图来实现

    今天就讲到这里,谢谢大家阅读,感谢支持!谢谢点赞

  • 相关阅读:
    Vue 导出excel 自适应宽度
    .Net 5.0 项目数据库连接字符串
    .Net 5.0 从api下载文件到本地
    Oracle for 循环输出(游标提取)
    找到多个与名为“Home”的控制器匹配的类型
    让tomcat使用指定JDK
    .NetCore 3 单文件发布详解
    CentOS7 常用命令大全
    阿里云ECS CentOS 7.8 安装图形化桌面GNOME
    用命令禁用本地连接
  • 原文地址:https://www.cnblogs.com/ws17345067708/p/10653097.html
Copyright © 2011-2022 走看看