包的安装在这里就不介绍了
以下代码都是在jupyter notebook中运行测试
这里采用的是离线模式
在线模式的使用请自行百度
下面直接进入主题
1. 散点、折线图
2. 柱状图
# -*- coding: utf-8 -*-
import plotly.offline as of
import numpy as np
import plotly.graph_objs as go
import plotly
# 生成1000个符合标准正态分布的数
random_x = np.random.randn(100)
random_y = np.random.randn(100)
# 传入数据来源,选择图形模式
trace = go.Scatter(
x = random_x,
y = random_y,
mode = 'markers',# 纯散点图
marker=dict(
size=10, # 设置点的宽度
color = 'rgba(255, 182, 193, .9)', # 设置点的颜色
line = dict( # 设置点外围的线的样式
width = 1,
color = 'blue'
)
)
)
'''
marker ----> 图形的样式,可单个数据使用或者字典
'''
# 传入绘图数据
data = [trace]
# 绘图
# of.plot(data)
'''将graph部分和layout部分组合成figure对象'''
fig = go.Figure(data=data)
'''启动绘图直接绘制figure对象'''
plotly.offline.init_notebook_mode()
plotly.offline.iplot(fig,filename='basic-scatter')
#上面两行代码可以用fig.show()代替,同样可以在jupyter中显示图表
# 生成1000个符合标准正态分布的数
random_x = np.linspace(0, 1, 100)
random_y = np.random.randn(100)-5
# 传入数据来源,选择图形模式
trace = go.Scatter(
x = random_x,
y = random_y,
mode = 'lines', # 折线图
name = 'lines',
line = dict(
color = 'rgba(255, 182, 193)',
width = 1
)
# marker=dict(
# size='16',
# color = np.random.randn(500)
# colorscale=colorscale,
# showscale=True
# )
)
'''
marker ----> 图形的样式,可单个数据使用或者字典
'''
# 传入绘图数据
data = [trace]
# 绘图
# of.plot(data)
'''将graph部分和layout部分组合成figure对象'''
fig = go.Figure(data=data)
'''启动绘图直接绘制figure对象'''
plotly.offline.init_notebook_mode()
plotly.offline.iplot(fig,filename='basic-scatter')
# 生成1000个符合标准正态分布的数
random_x = np.linspace(0, 1, 100)
random-y2 = np.random.randn(100)-5
# 传入数据来源,选择图形模式
trace = go.Scatter(
x = random_x,
y = random_y2,
mode = 'lines+markers', # 点线结合的模式
name = 'lines+markers'
)
# 传入绘图数据
data = [trace]
# 绘图
fig = go.Figure(data=data)
'''启动绘图直接绘制figure对象'''
plotly.offline.init_notebook_mode()
plotly.offline.iplot(fig)
#Scatter三张种基本画法
import plotly.graph_objs as go
import numpy as np
import plotly.offline as of
n = 100
random_x = np.linspace(0, 1, n)
random_y0 = np.random.randn(n)+5
random_y1 = np.random.randn(n)
random_y2 = np.random.randn(n)-5
trace0 = go.Scatter(
x = random_x,
y = random_y0,
mode = 'markers',
name = 'markers'
)
trace1 = go.Scatter(
x = random_x,
y = random_y1,
mode = 'lines',
name = 'lines'
)
trace2 = go.Scatter(
x = random_x,
y = random_y2,
mode = 'lines+markers',
name = 'lines+markers'
)
data = [trace0, trace1, trace2]
fig = go.Figure(data = data)
of.init_notebook_mode()
of.iplot(fig)
# -*- coding: utf-8 -*-
import plotly.offline as of
import plotly.graph_objs as go
trace = go.Bar(
x = ['a', 'b', 'c', 'd', 'e'],
y = [1, 3, 2, 5, -2]
)
# layout
layout = go.Layout(
title = 'basic Bar',
xaxis = {'title': 'x轴'} # 设置x轴, y同理
)
# Figure
data = [trace]
fig = go.Figure(data = data, layout = layout)
fig.show()
# 柱状簇
# Trace
trace_1 = go.Bar(
x = ["西南石油", "东方明珠", "海泰发展"],
y = [4.12, 5.32, 0.60],
name = '201609'
)
trace_2 = go.Bar(
x = ["西南石油", "东方明珠", "海泰发展"],
y = [3.65, 6.14, 0.58],
name = '201612'
)
trace_3 = go.Bar(
x = ["西南石油", "东方明珠", "海泰发展"],
y = [2.15, 1.35, 0.19],
name = '201703'
)
trace = [trace_1, trace_2, trace_3]
# Layout
layout = go.Layout(
title = '净资产收益率对比图'
)
# Figure
fig = go.Figure(data = trace, layout = layout)
fig.show()
关键参数:
layout中的barmode参数的值为’stack’
数据含有负数时使用相对堆叠柱状图比较合适
# Stacked Bar Chart
trace_1 = go.Bar(
x = ['深证50', '上证50', '西南50', '西北50','华中50'],
y = [0.7252, 0.9912, 0.5347, 0.4436, 0.9911],
name = '股票投资'
)
trace_2 = go.Bar(
x = ['深证50', '上证50', '西南50', '西北50','华中50'],
y = [0.2072, 0, 0.4081, 0.4955, 0.02],
name='其它投资'
)
trace_3 = go.Bar(
x = ['深证50', '上证50', '西南50', '西北50','华中50'],
y = [0, 0, 0.037, 0, 0],
name='债券投资'
)
trace_4 = go.Bar(
x = ['深证50', '上证50', '西南50', '西北50','华中50'],
y = [0.0676, 0.0087, 0.0202, 0.0609, 0.0087],
name='银行存款'
)
trace = [trace_1, trace_2, trace_3, trace_4]
layout = go.Layout(
title = '基金资产配置比例图',
barmode = 'stack' # 堆叠柱状图
)
fig = go.Figure(data = trace, layout = layout)
of.init_notebook_mode()
of.iplot(fig)
barmode ----> ‘stack’和’relative’
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': '相对堆叠条形图'
}
'''
barmode ----> 'stack'和'relative'
'''
fig = go.Figure(data = data, layout=layout)
fig.show()
底层悬空部分是完全透明的柱条
# 瀑布式柱状图
x_data = ['资产1', '资产2',
'资产3','资产4', '总资产']
y_data = [56000000, 65000000, 65000000, 81000000, 81000000]
text = ['666,999,888万元', '8,899,666万元', '88,899,666万元', '16,167,657万元', '888,888,888万元']
# Base
trace0 = go.Bar(
x=x_data,
y=[0, 57999848, 0, 66899764, 0],
marker=dict(
color='rgba(1,1,1, 0.0)',
),
name = '123'
)
# Trace
trace1 = go.Bar(
x=x_data,
y=[57999848, 8899916, 66899764,16167657, 83067421],
marker=dict(
color='rgba(55, 128, 191, 0.7)',
line=dict(
color='rgba(55, 128, 191, 1.0)',
width=2,
)
),
name='aaa'
)
data = [trace0, trace1]
layout = go.Layout(
title = '瀑布式柱状图',
barmode='stack',
showlegend=False
)
annotations = []
for i in range(0, 5):
annotations.append(dict(x=x_data[i], y=y_data[i], text=text[i], # 坐标和要注释的文字
font=dict(family='Arial', size=14, # 字体样式
color='red'),
showarrow=False,)) # 是否带有箭头
layout['annotations'] = annotations # 添加注释文字
fig = go.Figure(data=data, layout=layout)
of.init_notebook_mode()
of.iplot(fig)
# 柱状图颜色与样式的设置
volume = [0.49,0.71,1.43,1.4,0.93]
width = [each*3/sum(volume) for each in volume]
trace0 = go.Bar(
x = ['AU.SHF', 'AG.SHF', 'SN.SHF','PB.SHF', 'CU.SHF'],
y = [0.85, 0.13, -0.93, 0.46, 0.06],
width = width,
marker = dict(
color=['rgb(205,38,38)', 'rgb(205,38,38)','rgb(34,139,34)', 'rgb(205,38,38)','rgb(205,38,38)'],
line=dict(
color='rgb(0,0,0)',
width=1.5
)
),
opacity=0.8
)
data = [trace0]
layout = go.Layout(
title = '有色金属板块主力合约日内最高涨幅与波动率图',
xaxis=dict(tickangle=-45),
xaxis_rangeslider_visible=True # 显示x轴下方的可滑动视图
)
fig = go.Figure(data=data, layout=layout)
of.init_notebook_mode()
of.iplot(fig)