plotly是一个基于plotly.js构建的可视化工具库,支持2D、3D图形,交互流畅。相较于matplotlib这种基础绘图组件,plotly的可视化展现效果更好,可以媲美Tableau的高质量图。
一、plotly的名词解释 踪迹(trace):类似于matplotlib中的图形(例如折线),只管画图。 布局(layout):规定了一些需要辅助绘制的内容,如标题、图例等。 数据(data):就是一个踪迹的列表,因为可能会同时绘制多个图形,例如多条折线。 画布(figure):与matplotlib中的画布类似,需要传入data和layout参数。
二、plotly绘图步骤 step 1: 准备数据step 2: 创建一个或多个踪迹(traces)step 3: 同层踪迹合并step 4: 创建布局(layout)step 5: 在画布(figure)上组合graph部分和layout部分step 6: 图像显示/保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import plotly as pyimport plotly.graph_objs as goimport numpy as np if __name__ == '__main__' : x = [i for i in range(60 )] y_shanghai = [np.random.uniform(15 , 18 ) for i in x] y_beijing = [np.random.uniform(12 , 17 ) for i in x] trace1 = go.Scatter( x=x, y=y_shanghai, mode='markers' , name='shanghai' ) trace2 = go.Scatter( x=x, y=y_beijing, mode='markers' , name='beijing' ) data = [trace1, trace2] layout = go.Layout(title='示例' ) fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='first_offline_start.html' , auto_open=True )
plotly官方文档