一、导入包
# -*- 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
二、参数说明
本文用到的部分参数说明如下,仅供参考,具体见官方文档;
当只有1条数据时,默认不展示图例,即便是设置了name值;
showlegend(boolean) : 默认值True,只有1条数据时,需要设置为True后才能显示 name;
mode : 设置图表类型,可以设置:lines、markers、两者相加;
name : 设置图例项目名称,并在悬停时显示;
line : 设置折线类型,包括:color、width、shape、smoothing(平滑线)、dash(线条样式)、simplify(简化线条);
layout : 设置图表布局。title设置图表标题、xaxis和yaxis设置坐标轴标题、width和height设置图表的宽和高;
图例命名时,可以通过html标签加粗、添加空格等操作;
connectgaps:设置数据为空(即{nan}或缺失值)时,图表是否连接,默认为Fasle;
hoverinfo : 悬停时显示的跟踪信息,任何组合"x", "y", "z", "text", "name" ;
line. shape : 设置线条形状,enumerated : "linear" | "spline" | "hv" | "vh" | "hvh" | "vhv" ;
text : 关联的文本元素。单个字符串时,则在所有数据点上显示相同的字符串;字符串数组时,则跟踪(x,y)坐标元素而映射;
font : 设置悬停标签中使用的字体。family(HTML字体系列)、size、color等;
layout.legend : 设置图例布局
bgcolor:设置图例背景颜色
bordercolor:设置图例边框颜色
borderwidth:设置图例的边框的宽度
front:设置图例文本设置
orientation:设置图例方向
x : 设置图例的x位置,范围:-2~3,y轴相同
traceorder:设置图例显示顺序, 包括:"reversed","grouped","reversed+grouped","normal"
三、简单折线图
- 代码
trace = go.Scatter(
x = np.linspace(0, 1, 5),
y = np.random.randn(5),
showlegend=True
)
data = [trace]
py.iplot(data)
-
效果
四、多线图模式
- 代码
trace0 = go.Scatter(
x = np.linspace(0, 1, 100),
y = np.random.randn(100) + 5,
mode = 'lines',
name = 'line'
)
trace1 = go.Scatter(
x = np.linspace(0, 1, 100),
y = np.random.randn(100),
mode = 'lines + markers',
name = 'line + marker'
)
trace2 = go.Scatter(
x = np.linspace(0, 1, 100),
y = np.random.randn(100) - 5,
mode = 'markers',
name = 'marker'
)
data = [trace0, trace1, trace2]
py.iplot(data)
-
效果
五、风格线图
- 代码
month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December']
high_2007 = [32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3]
low_2007 = [13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9]
high_2014 = [36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0]
low_2014 = [23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6]
high_2018 = [28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9]
low_2018 = [12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1]
trace0 = go.Scatter(
x = month,
y = high_2018,
name = 'High 2018',
line = dict(color = ('rgb(205, 12, 24)'), width = 4)
)
trace1 = go.Scatter(
x = month,
y = low_2018,
name = 'Low 2018',
line = dict(color = ('rgb(22, 96, 167)'), width = 4)
)
trace2 = go.Scatter(
x = month,
y = high_2014,
name = 'High 2014',
line = dict(color = ('rgb(205, 12, 24)'), width = 4, dash = 'dash')
)
trace3 = go.Scatter(
x = month,
y = low_2014,
name = 'Low 2014',
line = dict(color = ('rgb(22, 96, 167)'), width = 4, dash = 'dash')
)
trace4 = go.Scatter(
x = month,
y = high_2007,
name = 'High 2007',
line = dict(color = ('rgb(205, 12, 24)'), width = 4, dash = 'dot')
)
trace5 = go.Scatter(
x = month,
y = low_2007,
name = 'Low 2007',
line = dict(color = ('rgb(22, 96, 167)'), width = 4, dash = 'dot')
)
data = [trace0, trace1, trace2, trace3, trace4, trace5]
layout = dict(title = '北京最高和最低的气温', xaxis = dict(title = '月份'), yaxis = dict(title = '气温 (°C)'))
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='styled-line')
-
效果
六、可间断折线图
- 代码
trace0 = go.Scatter(
x = [1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15],
y = [10, 20, None, 15, 10,
5, 15, None, 20, 10,
10, 15, 25, 20, 10],
name = '<b>No</b> Gaps', # Style name/legend entry with html tags
connectgaps=True
)
trace1 = go.Scatter(
x = [1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15],
y = [5, 15, None, 10, 5,
0, 10, None, 15, 5,
5, 10, 20, 15, 5],
name = 'Gaps'
)
data = [trace0, trace1]
py.iplot(data)
-
效果
七、线图插值
- 代码
trace1 = go.Scatter(
x = [1, 2, 3, 4, 5],
y = [1, 3, 2, 3, 1],
mode='lines+markers',
name="'linear'",
hoverinfo='name',
line=dict(shape='linear')
)
trace2 = go.Scatter(
x = [1, 2, 3, 4, 5],
y = [6, 8, 7, 8, 6],
mode='lines+markers',
name="'spline'",
text=["tweak", "line", "smoothness<br>with", "smoothing", "in line object"],
hoverinfo='text+name',
line=dict(shape='spline')
)
trace3 = go.Scatter(
x = [1, 2, 3, 4, 5],
y = [11, 13, 12, 13, 11],
mode='lines+markers',
name="'vhv'",
hoverinfo='name',
line=dict(shape='vhv')
)
trace4 = go.Scatter(
x = [1, 2, 3, 4, 5],
y = [16, 18, 17, 18, 16],
mode='lines+markers',
name="'hvh'",
hoverinfo='name',
line=dict(shape='hvh')
)
trace5 = go.Scatter(
x = [1, 2, 3, 4, 5],
y = [21, 23, 22, 23, 21],
mode='lines+markers',
name="'vh'",
hoverinfo='name',
line=dict(shape='vh')
)
trace6 = go.Scatter(
x = [1, 2, 3, 4, 5],
y = [26, 28, 27, 28, 26],
mode='lines+markers',
name="'hv'",
hoverinfo='name',
line=dict(shape='hv')
)
data = [trace1, trace2, trace3, trace4, trace5, trace6]
layout = dict(legend = dict(y=0.5, traceorder='reversed', font=dict(size=16)))
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='line-shapes')
-
效果