plotly系列(3)-- 绘制二维图表

plotly提供了常见二维图表的绘制。

一、数据分布

1、直方图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import numpy as np

x = [np.random.randint(1, 100) for i in range(100)] # 每个数字必须是整数

trace = go.Histogram(
x=x
)
data = [trace]

layout = go.Layout(title='示例')

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

2、箱形图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import numpy as np

y1 = np.random.randn(50)-1
y2 = np.random.randn(50)+1

trace1 = go.Box(
y=y1
)
trace2 = go.Box(
y=y2
)
data = [trace1, trace2]

layout = go.Layout(title='示例')

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

3、小提琴图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import numpy as np

y1 = np.random.randn(50)-1
y2 = np.random.randn(50)+1

trace1 = go.Violin(
y=y1
)
trace2 = go.Violin(
y=y2
)
data = [trace1, trace2]

layout = go.Layout(title='示例')

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

二、数据比较

1、柱状图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import plotly as py
import plotly.graph_objs as go
import numpy as np

trace1 = go.Bar(
x = ['Jan','Feb','Mar','Apr', 'May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'],
y = [20,14,25,16,18,22,19,15,12,16,14,17],
name = 'Primary Product',
marker=dict(
color = 'rgb(49,130,189)'
)
)
trace2 = go.Bar(
x = ['Jan','Feb','Mar','Apr', 'May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'],
y = [19,14,22,14,16,19,15,14,10,12,12,16],
name = 'Secondary Product',
marker=dict(
color = 'rgb(204,204,204)'
)
)
data = [trace1, trace2]

layout = go.Layout(title='示例')

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

2、雷达图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import plotly as py
import plotly.graph_objs as go

categories = ['processing cost','mechanical properties','chemical stability', 'thermal stability', 'device integration']

trace1 = go.Scatterpolar(
r=[1, 5, 2, 2, 3],
theta=categories,
fill='toself',
name='Product A'
)

trace2 = go.Scatterpolar(
r=[4, 3, 2.5, 1, 2],
theta=categories,
fill='toself',
name='Product B'
)
data = [trace1, trace2]

layout = go.Layout(title='示例')

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

3、折线图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import plotly as py
import plotly.graph_objs as go
import numpy as np

x = [i for i in range(100)]
y1 = np.random.randn(100)
y2 = np.random.randn(100)

trace1 = go.Scatter(
x=x,
y=y1,
mode='lines',
name='line1'
)
trace2 = go.Scatter(
x=x,
y=y2,
mode='lines',
name='line2'
)
data = [trace1, trace2]

layout = go.Layout(title='示例')

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

三、数据关系

1、散点图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import plotly as py
import plotly.graph_objs as go
import numpy as np

random_x = np.random.randn(1000)
random_y = np.random.randn(1000)

trace = go.Scatter(
x=random_x,
y=random_y,
mode='markers'
)
data = [trace]

layout = go.Layout(title='示例')

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

2、气泡图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import plotly as py
import plotly.graph_objs as go
import numpy as np

random_x = np.random.randn(1000)
random_y = np.random.randn(1000)

trace = go.Scatter(
x=random_x,
y=random_y,
mode='markers',
marker=dict(
size=16,
color=np.random.randn(500),
colorscale='Viridis',
showscale=True
)
)
data = [trace]

layout = go.Layout(title='示例')

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

四、数据成分

1、饼图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import numpy as np

labels = ['产品1','产品2','产品3','产品4','产品5']
values = [38.7,15.33,19.9,8.6,17.47]

trace = go.Pie(
labels=labels,
values=values,
hole=0.3, # 空白区域比例
hoverinfo="label + percent"
)
data = [trace]

layout = go.Layout(title='示例')

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

2、堆积柱形图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

import plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import numpy as np

trace1 = go.Bar(
y = ['CU.SHF', 'AG.SHF', 'AU.SHF'],
x = [21258, 30279, 8056],
name = '期货1',
orientation = 'h',
marker = dict(
color = '#104E8B',
line = dict(
color = '#104E8B',
width = 3)
)
)
trace2 = go.Bar(
y = ['CU.SHF', 'AG.SHF', 'AU.SHF'],
x = [19853, 9375, 4063],
name = '期货2',
orientation = 'h',
marker = dict(
color = '#1874CD',
line = dict(
color = '#104E8B',
width = 3)
)
)
trace3 = go.Bar(
y = ['CU.SHF', 'AG.SHF', 'AU.SHF'],
x = [4959, 13018, 8731],
name = '期货3',
orientation = 'h',
marker = dict(
color = '#1C86EE',
line = dict(
color = '#104E8B',
width = 3)
)
)

data = [trace1, trace2, trace3]

layout = go.Layout(
title='稀有金属期货持仓量对比图',
barmode='stack'
)

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

拓展阅读

0%