一、导入包
# -*- 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
二、参数说明
- 本文用到的部分参数说明如下,仅供参考,具体见官方文档;
- yaxis : 设置此迹线的y坐标和2D笛卡尔y轴之间的参考。xaxis类同;
- zeroline : 确定是否沿该轴的0值绘制直线。如果为“True”,则在网格线的顶部绘制零线;
- marker : 标记
opacity(0-1)标记透明度
size(标记大小)
color(标记颜色)
line(width:边界线宽度、color:marker.line颜色)
showscale : 仅当marker.color
设置为数值数组时才有效。确定是否显示颜色条;
colorscale : 设置色阶,在marker.line.color
设置为数值数组时才有效;
设置格式:1) [1, 'rgb(255,0,0)'];2)色板名称字符串。
三、简单散点图
N = 1000
random_x = np.random.randn(N)
random_y = np.random.randn(N)
trace = go.Scatter(
x = np.random.randn(500),
y = np.random.randn(500),
mode = 'markers'
)
data = [trace]
py.iplot(data)
四、风格散点图
trace0 = go.Scatter(
x = np.random.randn(N),
y = np.random.randn(N)+3,
name = 'Above',
mode = 'markers',
marker = dict(size = 10, color = 'rgba(152, 0, 0, .8)', line = dict(width = 2, color = 'rgb(0, 0, 0)'))
)
trace1 = go.Scatter(
x = np.random.randn(N),
y = np.random.randn(N)-2,
name = 'Below',
mode = 'markers',
marker = dict(size = 10, color = 'rgba(255, 182, 193, .9)', line = dict(width = 2))
)
data = [trace0, trace1]
layout = dict(title = '风格散点图', yaxis = dict(zeroline = False), xaxis = dict(zeroline = False))
fig = dict(data=data, layout=layout)
py.iplot(fig)
五、增加颜色刻度条
trace1 = go.Scatter(
y = np.random.randn(500),
mode='markers',
marker=dict(size=16, color = np.random.randn(500), colorscale='Viridis', showscale=True)
)
data = [trace1]
py.iplot(data)