plotly绘制简单图形<2>--条形图
程序员文章站
2022-06-01 18:34:47
...
之前说了散点图和折线图,本次主要说一下条形图:
1、简单条形图
import plotly.plotly as py
import plotly.graph_objs as go
data = [go.Bar(
x=['A', 'B', 'C'],
y=[20, 14, 23]
)]
py.iplot(data, filename='basic-bar')
2、分组条形图
import plotly.plotly as py
import plotly.graph_objs as go
trace1 = go.Bar(
x=['A', 'B', 'C'],
y=[20, 14, 23],
name='SF Zoo'
)
trace2 = go.Bar(
x=['A', 'B', 'C'],
y=[12, 18, 29],
name='LA Zoo'
)
data = [trace1, trace2]
layout = go.Layout(
barmode='group'#分组
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='grouped-bar')
3、堆积条形图
import plotly.plotly as py
import plotly.graph_objs as go
trace1 = go.Bar(
x=['A', 'B', 'C'],
y=[20, 14, 23],
name='SF Zoo'
)
trace2 = go.Bar(
x=['A', 'B', 'C'],
y=[12, 18, 29],
name='LA Zoo'
)
data = [trace1, trace2]
layout = go.Layout(
barmode='stack'
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='stacked-bar')
4、悬停文本
import plotly.plotly as py
import plotly.graph_objs as go
trace0 = go.Bar(
x=['A', 'B', 'C'],
y=[20, 14, 23],
text=['27% market share', '24% market share', '19% market share'],
marker=dict(
color='rgb(158,202,225)',
line=dict(
color='rgb(8,48,107)',
width=2.5,#线宽
)
),
opacity=0.5#透明度
)
data = [trace0]
layout = go.Layout(
title='January 2013 Sales Report',
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='text-hover-bar')
5、数据标签
import plotly.plotly as py
import plotly.graph_objs as go
x = ['A', 'B', 'C']
y = [20, 14, 23]
data = [go.Bar(
x=x,
y=y,
text=y,
textposition = 'auto',
marker=dict(
color='rgb(158,202,225)',
line=dict(
color='rgb(8,48,107)',
width=1.5),
),
opacity=0.6
)]
py.iplot(data, filename='bar-direct-labels')
6、瀑布图
import plotly.plotly as py
import plotly.graph_objs as go
x_data = ['A', 'B', 'C','D']
y_data = [20, 30, 30,25]
text=['20','10','30','-5']
# Base
trace0 = go.Bar(
x=x_data,
y=[0, 20, 0, 25],
marker=dict(
color='rgba(1,1,1, 0.0)',
)
)
# Revenue
trace1 = go.Bar(
x=x_data,
y=[20, 10, 30, 5],
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='Profit- 2018',
barmode='stack',
paper_bgcolor='rgba(245, 246, 249, 1)',
plot_bgcolor='rgba(245, 246, 249, 1)',
showlegend=False
)
annotations = []
for i in range(0, 4):
annotations.append(dict(x=x_data[i], y=y_data[i], text=text[i],
font=dict(family='Arial', size=24,
color='rgba(55, 60, 100, 1)'),
showarrow=False,))
layout['annotations'] = annotations
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='waterfall-bar-profit')