一、导入包
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import plotly.offline as py #保存图表,相当于plotly.plotly as py,同时增加了离线功能
py.init_notebook_mode(connected=True) #离线绘图时,需要额外进行初始化
import plotly.graph_objs as go #创建各类图表
import plotly.figure_factory as ff #创建table
二、参数说明
- 本文用到的部分参数说明如下,仅供参考,具体见官方文档;
- barmode : 设置相同坐标的条形图位置。包括 : stack(叠加)、group(并列)、overlay(覆盖)、relative(相对);
- opacity : 设置跟踪的不透明度,default为1;
- text : 设置跟踪文本;
- textposition : 设置text的位置,包括 : "inside" | "outside" | "auto" | "none";
- tickangle : 设置刻度标签相对于水平线的角度;
- base : 设置坐标位置基数;
- tickfont : 设置刻度标签字体和颜色;
- titlefont : 设置刻度标题字体和颜色;
- bargroupgap : 设置相同位置条形图之间的间隙,范围:0-1;
- bargap : 设置相邻位置条形图之间的间隙,范围:0-1;
- layout.legend : 设置图例布局
bgcolor:设置图例背景颜色
bordercolor:设置图例边框颜色
borderwidth:设置图例的边框的宽度
front:设置图例文本设置
orientation:设置图例方向
x : 设置图例的x位置,范围:-2~3,y轴相同
traceorder:设置图例显示顺序, 包括:"reversed","grouped","reversed+grouped","normal"
- <br> : 通过html标签可以对图例文本进行换行;
- paper_bgcolor : 设置图表内容外的背景色;
- plot_bgcolor : 设置图表背景色;
- font : 设置悬停标签中使用的字体。family(HTML字体系列)、size、color等;
- 特别地,Plotly的图表中,trace / data / layout / iplot 等均可以采用字典的方式书写。
三、基本条形图
trace = go.Bar(
x = ['giraffes', 'orangutans', 'monkeys'],
y = [15, 32, 26]
)
data = [trace]
py.iplot(data, filename='basic-bar')
-
效果
三、分组条形图
trace1 = go.Bar(
x = ['giraffes', 'orangutans', 'monkeys'],
y = [20, 14, 23],
name='SF Zoo'
)
trace2 = go.Bar(
x = ['giraffes', 'orangutans', 'monkeys'],
y = [12, 18, 29],
name='LA Zoo'
)
data = [trace1, trace2]
# layout = go.Layout(barmode='relative')
# fig = go.Figure(data=data, layout=layout)
layout = dict(barmode='stack')
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='grouped-bar')
-
效果
四、条形图与悬停文本
trace = go.Bar(
x = ['Product A', 'Product B', 'Product 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=1.5)),
opacity=0.6
)
data = [trace]
layout = dict(title='January 2013 Sales Report')
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='text-hover-bar')
-
效果
五、带有直接标签的条形图
x = ['Product A', 'Product B', 'Product C']
y = [20, 14, 23]
trace = go.Bar(
x = x,
y = y,
text = y,
textposition = 'outside',
marker = dict(color = 'rgb(158,202,225)',
line = dict(color = 'rgb(8,48,107)',
width = 1.5)
),
opacity = 0.6
)
data = [trace]
py.iplot(data, filename='bar-direct-labels')
-
效果
六、旋转条形图
trace0 = 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)')
)
trace1 = 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 = [trace0, trace1]
layout = dict(xaxis = dict(tickangle = -60), barmode = 'group')
fig = dict(data = data, layout = layout)
py.iplot(fig, filename = 'angled-text-bar')
-
效果
七、自定义单个条形颜色
trace0 = go.Bar(
x = ['Feature A', 'Feature B', 'Feature C', 'Feature D', 'Feature E'],
y = [20, 14, 23, 25, 22],
marker = dict(color=['rgba(204,204,204,1)', 'rgba(222,45,38,0.8)',
'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
'rgba(204,204,204,1)'])
)
data = [trace0]
layout = dict(title='Least Used Feature')
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='color-bar')
-
效果
八、自定义单个条宽度
trace0 = go.Bar(
x = [1, 2, 3, 5.5, 10],
y = [10, 8, 6, 4, 2],
width = [0.8, 0.8, 0.8, 3.5, 5]
)
data = [trace0]
py.iplot(data, filename='width-bar')
-
效果
九、自定义坐标位置基数
trace0 = go.Bar(
x = ['2016','2017','2018'],
y = [500,600,700],
base = [-500,-600,-700],
marker = dict(color = 'red'),
name = 'expenses'
)
trace1 = go.Bar(
x = ['2016','2017','2018'],
y = [300,400,700],
base = 0,
marker = dict(color = 'blue'),
name = 'revenue'
)
data = [trace0, trace1]
py.iplot(data)
-
效果
十、样式条形图
trace1 = go.Bar(
x = [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012],
y = [219, 146, 112, 127, 124, 180, 236, 207, 236, 263,
350, 430, 474, 526, 488, 537, 500, 439],
name = 'Rest of world',
marker = dict(color='rgb(55, 83, 109)')
)
trace2 = go.Bar(
x = [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012],
y = [16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156, 270,
299, 340, 403, 549, 499],
name = 'China',
marker = dict(color='rgb(26, 118, 255)')
)
data = [trace1, trace2]
layout = dict(title = 'US Export of Plastic Scrap',
xaxis = dict(title = 'Month',
tickfont = dict(size = 16, color = 'rgb(55, 83, 109)'),
titlefont = dict(size = 13, color = 'rgb(26, 118, 255)')),
yaxis = dict(title = 'USD (millions)',
tickfont = dict(size = 16, color = 'rgb(26, 118, 255)'),
titlefont = dict(size = 13, color = 'rgb(55, 83, 109)')),
legend = dict(x = 0.03,
y = 1.03,
bgcolor = 'rgba(204,204,204,1)',
bordercolor = 'rgba(222,45,38,0.8)',
borderwidth = 2),
barmode = 'group',
bargap = 0.3,
bargroupgap = 0.2)
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='style-bar')
-
代码
十一、瀑布条形图
x_data = ['Product<br>Revenue', 'Services<br>Revenue', 'Total<br>Revenue', 'Fixed<br>Costs',
'Variable<br>Costs', 'Total<br>Costs', 'Total']
y_data = [400, 660, 660, 660, 450, 660, 350]
text = ['$430K', '$260K', '$690K', '$-120K', '$-200K', '$-320K', '$370K']
# Base
trace0 = go.Bar(
x = x_data,
y = [0, 430, 0, 570, 370, 370, 0],
marker = dict(color='rgba(1,1,1, 0.0)') #通过颜色设置,将trace0的bar都隐藏了
)
# Revenue
trace1 = go.Bar(
x = x_data,
y = [430, 260, 690, 0, 0, 0, 0],
marker = dict(color='rgba(55, 128, 191, 0.7)',
line=dict(color='rgba(55, 128, 191, 1.0)', width=2))
)
# Costs
trace2 = go.Bar(
x = x_data,
y = [0, 0, 0, 120, 200, 320, 0],
marker = dict(color = 'rgba(219, 64, 82, 0.7)',
line=dict(color='rgba(219, 64, 82, 1.0)', width=2))
)
# Profit
trace3 = go.Bar(
x = x_data,
y = [0, 0, 0, 0, 0, 0, 370],
marker = dict(color='rgba(50, 171, 96, 0.7)',
line=dict(color='rgba(50, 171, 96, 1.0)', width=2))
)
data = [trace0, trace1, trace2, trace3]
layout = go.Layout(title='Annual Profit- 2015',
barmode='stack',
paper_bgcolor='rgba(204,204,204,1)',
plot_bgcolor='rgba(245, 246, 249, 1)',
showlegend=False)
"""
通过遍历,设置标记在图表中的位置:x 定位横轴、y 定位纵轴距离
"""
annotations = []
for i in range(0, 7):
annotations.append(dict(x = x_data[i],
y = y_data[i],
text = text[i],
font = dict(family='Arial', size=14,color='rgba(245, 246, 249, 1)'),
showarrow=False))
layout['annotations'] = annotations
fig = dict(data=data, layout=layout)
py.iplot(fig)
-
效果
十二、相对堆叠的条形图
trace1 = {
'x': [1, 2, 3, 4],
'y': [1, 4, 9, 16],
'name': 'Trace1',
'type': 'bar'
}
trace2 = {
'x': [1, 2, 3, 4],
'y': [6, -8, -4.5, 8],
'name': 'Trace2',
'type': 'bar'
}
trace3 = {
'x': [1, 2, 3, 4],
'y': [-15, -3, 4.5, -8],
'name': 'Trace3',
'type': 'bar'
}
trace4 = {
'x': [1, 2, 3, 4],
'y': [-1, 3, -3, -4],
'name': 'Trace4',
'type': 'bar'
}
data = [trace1, trace2, trace3, trace4]
layout = {
'xaxis': {'title': '横轴'},
'yaxis': {'title': '纵轴'},
'barmode': 'relative',
'title': '相对堆叠条形图'
};
py.iplot({'data': data, 'layout': layout}, filename='barmode-relative')
-
效果
网友评论