本文实例为大家分享了python绘制水平条形图的具体代码,供大家参考,具体内容如下
水平条形图与绘制柱状图类似,大家可以先看看我之前写的博客,如何绘制柱状图
水平条形图需要在bar函数中设置orientation= 'h'
其他的参数与柱状图相同。也可以通过设置barmode = 'stack',
绘制层叠水平条形图和瀑布式水平条形图
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import plotly as py
import plotly.graph_objs as go
pyplt = py.offline.plot
data = [go.bar(
x = [ 29.41 , 34.62 , 30.16 ],
y = [ '资产1' , '资产2' , '资产3' ],
orientation = 'h'
)]
layout = go.layout(
title = '净资产收益率对比'
)
figure = go.figure(data = data, layout = layout)
pyplt(figure, filename = 'tmp/1.html' )
|
运行上述代码,得到如上图所示的图例,可以看到其画法跟柱状图一样,只是变成水平方向。
如何画水平的层叠条形图,只需要我们将参数,barmode = 'stack',即可画出响应的水平图
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
|
import plotly as py
import plotly.graph_objs as go
pyplt = py.offline.plot
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)
pyplt(fig, filename = 'tmp/2.html' )
|
运行上述代码,可以得到如上图所示的层叠水平条形图。
水平条形图和柱状图的画法基本上相同。剩下的就不细讲了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u012798683/article/details/88814486