使用Plotly繪制基本的柱狀圖,需要用到的函數是graph_objs 中 Bar函數
通過參數,可以設置柱狀圖的樣式。
通過barmod進行設置可以繪制出不同類型的柱狀圖出來。
我們先來實現一個簡單的柱狀圖:
# -*- coding: utf-8 -*- import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot # Trace trace_basic = [go.Bar( x = ['Variable_1', 'Variable_2', 'Variable_3','Variable_4','Variable_5'], y = [1, 2, 3, 2, 4], )] # Layout layout_basic = go.Layout( title = 'The Graph Title', xaxis = go.XAxis(range = [-0.5,4.5], domain = [0,1]) ) # Figure figure_basic = go.Figure(data = trace_basic, layout = layout_basic) # Plot pyplt(figure_basic, filename='tmp/1.html')
上面這個例子,就是一個簡單的柱狀圖。
下面我們講下另外一種圖,柱狀簇
實現過程則是,在基本的柱狀圖中,加入多租數據即可實現,柱狀簇
import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot # Traces trace_1 = go.Bar( x = ["西南石油", "東方明珠", "海泰發展"], y = [4.12, 5.32, 0.60], name = "201609" ) trace_2 = go.Bar( x = ["西南石油", "東方明珠", "海泰發展"], y = [3.65, 6.14, 0.58], name = "201612" ) trace_3 = go.Bar( x = ["西南石油", "東方明珠", "海泰發展"], y = [2.15, 1.35, 0.19], name = "201703" ) trace = [trace_1, trace_2, trace_3] # Layout layout = go.Layout( title = '凈資產收益率對比圖' ) # Figure figure = go.Figure(data = trace, layout = layout) # Plot pyplt(figure, filename='tmp/2.html')
執行上述代碼,我們可以看到如上圖所示柱狀簇圖例
可將數據堆疊生成。
接下來在講講如何繪制層疊柱狀圖
層疊柱狀圖的繪制方法與柱狀簇的繪制方法基本差不多
也就是對同一個柱狀簇進行疊加,實現方法是對Layout中的barmode屬性進行設置
barmode = 'stack'
其余參數,與柱狀簇相同。
# -*- coding: utf-8 -*- import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot # Stacked Bar Chart trace_1 = go.Bar( x = ['深證50', '上證50', '西南50', '西北50','華中50'], y = [0.7252, 0.9912, 0.5347, 0.4436, 0.9911], name = '股票投資' ) trace_2 = go.Bar( x = ['深證50', '上證50', '西南50', '西北50','華中50'], y = [0.2072, 0, 0.4081, 0.4955, 0.02], name='其它投資' ) trace_3 = go.Bar( x = ['深證50', '上證50', '西南50', '西北50','華中50'], y = [0, 0, 0.037, 0, 0], name='債券投資' ) trace_4 = go.Bar( x = ['深證50', '上證50', '西南50', '西北50','華中50'], y = [0.0676, 0.0087, 0.0202, 0.0609, 0.0087], name='銀行存款' ) trace = [trace_1, trace_2, trace_3, trace_4] layout = go.Layout( title = '基金資產配置比例圖', barmode='stack' ) fig = go.Figure(data = trace, layout = layout) pyplt(fig, filename='tmp/1.html')
瀑布式柱狀圖
瀑布式柱狀圖是層疊柱狀圖的另外一種表現
可以選擇性地顯示層疊部分來實現柱狀圖的懸浮效果。
# -*- coding: utf-8 -*- import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot x_data = ['資產1', '資產2', '資產3','資產4', '總資產'] y_data = [56000000, 65000000, 65000000, 81000000, 81000000] text = ['666,999,888萬元', '8,899,666萬元', '88,899,666萬元', '16,167,657萬元', '888,888,888萬元'] # Base trace0 = go.Bar( x=x_data, y=[0, 57999848, 0, 66899764, 0], marker=dict( color='rgba(1,1,1, 0.0)', ) ) # Trace trace1 = go.Bar( x=x_data, y=[57999848, 8899916, 66899764,16167657, 83067421], marker=dict( color='rgba(55, 128, 191, 0.7)', line=dict( color='rgba(55, 128, 191, 1.0)', width=2, ) ) ) data = [trace0, trace1] layout = go.Layout( title = '測試圖例', barmode='stack', showlegend=False ) annotations = [] for i in range(0, 5): annotations.append(dict(x=x_data[i], y=y_data[i], text=text[i], font=dict(family='Arial', size=14, color='rgba(245, 246, 249, 1)'), showarrow=False,)) layout['annotations'] = annotations fig = go.Figure(data=data, layout=layout) pyplt(fig, filename = 'tmp/1.html')
運行上述代碼,可以得到如上圖所示的瀑布式柱狀圖。
下面我們說說,圖形樣式的設置。
對於柱狀圖顏色與樣式的設置可以通過設置下面這個案例來說明。
import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot # Customizing Individual Bar Colors volume = [0.49,0.71,1.43,1.4,0.93] width = [each*3/sum(volume) for each in volume] trace0 = go.Bar( x = ['AU.SHF', 'AG.SHF', 'SN.SHF', 'PB.SHF', 'CU.SHF'], y = [0.85, 0.13, -0.93, 0.46, 0.06], width = width, marker = dict( color=['rgb(205,38,38)', 'rgb(205,38,38)', 'rgb(34,139,34)', 'rgb(205,38,38)', 'rgb(205,38,38)'], line=dict( color='rgb(0,0,0)', width=1.5, )), opacity = 0.8, ) data = [trace0] layout = go.Layout( title = '有色金屬板塊主力合約日內最高漲幅與波動率圖', xaxis=dict(tickangle=-45), ) fig = go.Figure(data=data, layout=layout) pyplt(fig, filename='tmp/4.html')
運行上述代碼,可以看到上圖所示圖例
柱狀圖展示了5種金屬,在某個交易日的最高漲幅與波動率情況,柱形圖寬度表示相對波動率的高低
柱形圖越寬,波動率越大,高度表示漲幅,紅色表示上漲,綠色表示下跌。
用line設置柱狀圖外部線框,用width設置柱狀圖的寬度,用opacity設置柱狀圖顏色的透明度情況。
基本的柱狀圖情況,就講到這里。