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模式,而繪制堆積面積圖時則不需要

這就是堆積面積圖需要累加數據的原因

在本質上,堆積面積圖的堆積效果是在同一個圖形中繪制對個面積圖來實現

今天就講到這里,謝謝大家閱讀,感謝支持!謝謝點贊

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM