Plotly绘图指令速查 – pyhton

线上运行以及绘图效果,欢迎移步和鲸社区

基本步骤

1.安装

#terminal里

pip install plotly

2.帮助页面

http://www.plot.ly/python/getting-started

3.导入库

import plotly.offline as py

import plotly.graph_objs as go

4.hello world

trace = {‘x’:[1,2], ‘y’:[1,2]}

data = [trace]

layout = {}

fig = go.Figure(

    data = data, layout = layout)

5.展示图象

py.offline.plot(fig)

基础图表

折线图

trace1 = go.Scatter(

    x = [1,2], y = [1,2])

trace2 = go.Scatter(

    x = [1,2], y = [2,2])

py.iplot([trace1,trace2])

散点图

trace1 = go.Scatter(

    x = [1,2,3], y = [1,2,3],

    text = [‘A’,’B’,’C’],

    textposition = ‘top center’,

    mode = ‘markers+text’)

data = [trace]

py.iplot(data)

条形图

trace = go.Bar(

    x = [1,2],

    y = [1,2],)

data = [trace]

py.iplot(data)

气泡图

trace = go.Scatter(

    x = [1,2,3],

    y = [1,2,3],

    marker = dict(

        color = [‘red’,’blue’,’green’],

        size = [30,80,200]),

    mode = ‘markers’

)

py.iplot([trace])

热力图

trace = go.Heatmap(

    z = [[1,2,3,4],

    [5,6,7,8]])

data = [trace]

py.iplot(data)

面积图

trace = go.Scatter(

    x = [1,2,6],

    y = [1,2,0.5],

    fill = “tonexty”)

data = [trace]

py.iplot(data)

样式

图例legends

trace1 = go.Scatter(

    name = “Calvin”,

    x = [1,2],

    y = [2,1])

trace2 = go.Scatter(

    name = “Hobbes”,

    x = [2,1],

    y = [2,1])

layout = go.Layout(

    showlegend = True,

    # 设置图例相对于左下角的位置

    legend = dict(

        x = 0.2,

        y = 0.5))

data = [trace1, trace2]

fig = go.Figure(data = data, layout = layout)

py.iplot(fig)

坐标 axes

trace = go.Scatter(

    x = [-1,1,2,3,4],

    y = [-1,1,2,3,6])

axis_template = dict(

    showgrid = True,  #网格

    zeroline = True,  #是否显示基线,即沿着(0,0)画出x轴和y轴

    nticks = 20,

    showline = True,

    title = ‘X axis’,

    mirror = ‘all’)

layout = go.Layout(

    xaxis = axis_template,

    yaxis = axis_template)

data = [trace]

fig = go.Figure(

    data = data,

    layout = layout)

py.iplot(fig)

数据类图表

直方图

trace = go.Histogram(

    x = [1,2,3,3,3,4,5])

data = [trace]

py.iplot(data)

箱型图

trace=go.Box(

    x=[1,2,3,3,3,4,5])

data=[trace]

py.iplot(data)

二维直方图

trace=go.Histogram2d(

    x=[1,2,3,3,3,4,5],

    y=[1,2,3,3,3,4,5])

data=[trace]

py.iplot(data)

地图

气泡地图

trace = dict(

    type = ‘scattergeo’,

    lon = [100,400],lat = [0,0],

    marker = dict(

        color = [‘red’,’blue’],

        size = [30,50]),

    mode = ‘markers’)

py.iplot([trace])

分级统计地图

import plotly.colors

trace = dict(

    type = ‘choropleth’,

    locations = [‘AZ’,’CA’,’VT’],

    locationmode = ‘USA-states’,

    colorscale = ‘Viridis’,

    z = [10,20,40])

layout = dict(geo = dict(scope = ‘usa’))

map = go.Figure(data = [trace], layout = layout)

py.iplot(map)

三维图

三维平面图

trace=go.Surface(

    colorscale=’Viridis’,

    z=[[3,5,8,13],

    [21,13,8,5]])

data = [trace]

py.iplot(data)

三维折线图

trace = go.Scatter3d(

    x = [9,8,5,1],

    y = [1,2,4,8],

    z = [11,8,15,3],

    mode = ‘lines’)

data = [trace]

py.iplot(data)

三维散点图

trace = go.Scatter3d(

    x = [9,8,5,1],

    y = [1,2,4,8],

    z = [11,8,15,3],

    mode = ‘markers’)

data = [trace]

py.iplot(data)

数据结构

Figure {}

data []

  trace {}

  x,y,z []

  color,text,size []

  colorscale ABC or []

  marker {}

    color ABC

    symbol ABC

    line {}

    color ABC

    width 123

layout {}

title ABC

xaxis,yaxis {}

scene {}

  xaxis,yaxis,zaxis {}

geo {}

legend {}

annotations {}

{} 字典

[] 列表

ABC 字符

123 数字

参考资料:

plotly使用指南

plot.ly

转载本文请联系和鲸取得授权,和鲸社区是聚合数据人才和行业问题的在线社区,率先打造国内首款K-Lab 在线数据分析协作平台,为数据工作者的学习与工作带来全新的体验。

https://www.jianshu.com/p/fbe67bb16ef2

Python量化投资网携手4326手游为资深游戏玩家推荐:《小米赛车下载

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论