Plotly-express-23-绘制水平柱状图
本文中介绍的是如何利用Plotly_express绘制水平方向的柱状图
- 单组水平柱状图
- 多组水平柱状图
单组水平柱状图
效果
代码
代码语言:javascript复制import plotly as py
import plotly.graph_objs as go
data = [go.Bar(
x=[21, 38, 30],
y=['小明', '小红', '小军'],
orientation = 'h' # 改成h
)]
layout = go.Layout(
title = '年龄'
)
fig = go.Figure(data = data, layout = layout)
fig.show()
多组水平柱状图
效果
代码
代码语言:javascript复制import plotly as py
import plotly.graph_objs as go
pyplt = py.offline.plot
trace1 = go.Bar(
y = ['语文', '数学', '英语'],
x = [88, 79, 86],
name = '张三',
orientation = 'h',
marker = dict(
color = '#104E8B',
line = dict(
color = '#104E8B',
width = 3)
)
)
trace2 = go.Bar(
y = ['语文', '数学', '英语'],
x = [98, 75, 63],
name = '小明',
orientation = 'h',
marker = dict(
color = '#1874CD',
line = dict(
color = '#104E8B',
width = 3)
)
)
trace3 = go.Bar(
y = ['语文', '数学', '英语'],
x = [79, 88, 97],
name = '小军',
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)
fig.show()