【6】网络与网页-6-可视化交互--Plotly

Plotly可以将数据在网页上显示,并有一定的交互。

教学:https://help.plot.ly/tutorials/

一、安装

pip install plotly

更新

pip install plotly --upgrade

二、在线使用,画的图会存入云账户中

import plotly

#设置用户名和API-Key
plotly.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81')

三、离线使用

import plotly
from plotly.graph_objs import Scatter, Layout

plotly.offline.init_notebook_mode(connected=True)

py.plot和py.iplot区别

py.plot会生成一个离线的html文件,里面放置图片。而py.iplot则直接在ipython notebook里面生成图片。

3.1 散点图和折线图

基本散点图:

import plotly.offline as py
from plotly.graph_objs import Scatter, Layout
import plotly.graph_objs as go

py.init_notebook_mode(connected=True)

trace1=go.Scatter(
    x=[1,2],
    y=[1,2]
)
trace2=go.Scatter(
    x=[1,2],
    y=[2,1]
)
py.iplot([trace1, trace2])

可以改变mode,换成散点图:

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.Scatter(
    x=[1,2,3],
    y=[1,2,3],
    marker=dict(
        color=['red','blue','green'],
        size=[30,80,200],
    ),
     mode='markers+lines'
)
py.iplot([trace])

3.2 设置图例和图例位置

trace1=go.Scatter(
    name="lenged1",
    x=[1,2],
    y=[2,1]
)
trace2=go.Scatter(
    name="lenged2",
    x=[2,1],
    y=[2,1]
)
layout=go.Layout(
    showlegend=True,
    # 设置图例相对于左下角的位置
    legend=dict(
        x=0.9,
        y=1.1
    )
)
data=[trace1, trace2]
fig=go.Figure(data=data, layout=layout)
py.iplot(fig)

为点加上文字:

trace1=go.Scatter(
    x=[1,2,3],
    y=[1,2,3],
    text=['A', 'B', 'C'],
    textposition="top center",
    mode="markers+text",
    marker=dict(
        size=[20,20,20]
    )
)
data=[trace1]
py.iplot(data)

更多设置: plotly.Scatter设置: https://plot.ly/python/reference/#scatter

3.3 设置坐标轴

trace=go.Scatter(
    x=[1,2,3,4],
    y=[1,2,3,6]
)
axis_template=dict(
    showgrid=True,  #网格
    zeroline=True,  #是否显示基线,即沿着(0,0)画出x轴和y轴
    nticks=20,
    showline=True,
    title='X axis',
    mirror='all',
    zerolinecolor="#FF0000"
)
layout=go.Layout(
    xaxis=axis_template,
    yaxis=axis_template
)
data=[trace]
fig=go.Figure(
    data=data,
    layout=layout
)
py.iplot(fig)

更多关于坐标轴设置的参数详见: plotly坐标轴设置 https://plot.ly/python/reference/#layout-xaxis-zerolinecolor

3.4 Heatmap

trace=go.Heatmap(
    z=[
        [1,2,3,4],
        [5,6,7,8]
    ]
)
data=[trace]
py.iplot(data)

3.5 Barchart

trace=go.Bar(
    x=[1,2],
    y=[1,2],
    marker=dict(
        color=["#FF0000", "#00FF00"],
    )
)
data=[trace]
py.iplot(data)

更多设置:plotly.Bar https://plot.ly/python/reference/#bar

3.6 填充区域

trace=go.Scatter(
    x=[1,2,6],
    y=[1,2,0.5],
    fill="tonexty",
    fillcolor="#FF0"
)
data=[trace]
py.iplot(data)

3.7 直方图

trace=go.Histogram(
    x=[1,2,3,3,3,4,5],
    marker=dict(
        color=["#F0F","FF0","0FF"]
    )
)
data=[trace]
py.iplot(data)

3.8 画地图

trace=go.Scattergeo(
   lon=[100, 400],
    lat=[0,0],
    marker=dict(
        color=['red', 'blue'],
        size=[30,50]
    ),
    mode="markers"
)
data=[trace]
py.iplot(data)

3.9 3D表面图

trace=go.Surface(
    colorscale='Viridis',
    z=[
        [3,5,8,13],
        [21,13,8,5]
    ]
)
data=[trace]
py.iplot(data)

3.10 箱型图(Box-plot)

trace=go.Box(
    x=[1,2,3,3,3,4,5]
)
data=[trace]
py.iplot(data)

3.11 画地图

trace=go.Choropleth(
    locations=['AZ','CA','VT'],
    locationmode='USA-states',
    colorscale=['Viridis'],
    z=[10,20,40]
)
layout=dict(
    geo=dict(scope='usa')
)
data=[trace]
map=go.Figure(data=data, layout=layout)
py.iplot(map)

3.12 3D点图

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)

3.13 2维Historgram

trace=go.Histogram2d(
    x=[1,2,3,3,3,4,5],
    y=[1,2,3,3,3,4,5]
)
data=[trace]
py.iplot(data)

参考资料

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn

Sam avatar
About Sam
专注生物信息 专注转化医学