- 箱线图
- 饼图
3.箱线图
import plotly.offline as of
import plotly.graph_objs as go
x0 = np.random.randn(50)
x1 = np.random.randn(50)
colors = ['#faee1c', '#f3558e']
trace0 = go.Box(
x = x0,
name = 'box1',
marker = {'color': colors[0]}
)
trace1 = go.Box(
x = x1,
name = 'box2',
boxpoints = 'all', # 显示出每一个点
jitter = 0.3, # 点在y轴上的随机抖动距离
pointpos = -1.8, # 点在y轴上的偏移距离
marker = dict(
color = colors[3],
line = dict(
color = 'red',
widh = 1
)
)
)
data = [trace0, trace1]
layout = go.Layout(
title = '箱线图测试'
)
fig = go.Figure(data=data, layout=layout)
of.init_notebook_mode()
of.iplot(fig)
4.饼图
import plotly.offline as of
import plotly.graph_objs as go
labels = ['产品1', '产品2','产品3', '产品4', '产品5']
values = [30, 25, 15, 22, 2]
colors = ['#FFFF00', '#FF0000', '#E066FF', '#0D0D0D']
trace = [
go.Pie(
labels = labels, # 每一部分的标签列表
values = values, # 每一部分的值
rotation = 30,
showlegend = False,
pull = [0.1, 0, 0, 0, 0],
textinfo = 'label+value', # 'percent'
hoverinfo = 'label+value+percent',
textfont = dict(size=20, color='blue'),
marker = dict(
colors = colors,
line = dict(
color =['blue','black','red','yellow','green'],
width = 2
)
)
)
]
'''
colors ----> 在marker里,设置每一块label的颜色
rotation ----> 饼图旋转的角度
opacity ----> 图表的透明度
showlegend ----> 是否显示图例
pull ----> 相当于matplotlib的explode,突出显示
textinfo ----> 每一块图中显示的内容,参数有value,percent,label
hoverinfo ----> 鼠标经过显示的信息
label - 显示对应的label值
value - 显示对应的value值
percent - 显示对应的百分比
textfont ----> 每一块图中显示的字体的样式参数
'''
layout = go.Layout(
title = '产品比例配比图',
)
fig = go.Figure(data=trace, layout=layout)
of.init_notebook_mode()
of.iplot(fig)
如果要画空心的饼图,需要用到hole参数
例子:
labels = ['完成', '未完成']
values = [0.8, 0.2]
trace = [
go.Pie(
labels = labels,
values = values,
hole = 0.7 # 空心的比例
)
layout = go.Layout(
title = '产品完成进度图'
)
fig = go.Figure(data=trae, layout=layout)
of.init_notebook_mode()
of.iplot(fig)