plotly是开挂的作图神器,可以供js, python, R, DB等使用。具体见官网plotly官网
1. 安装
pip install plotly
- 1
更新
pip install plotly --upgrade
- 1
2. 在线使用,画的图会存入云账户中
import plotly#设置用户名和API-Key
plotly.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81')
- 1
- 2
- 3
- 4
3. 离线使用
import plotly
from plotly.graph_objs import Scatter, Layoutplotly.offline.init_notebook_mode(connected=True)
- 1
- 2
- 3
- 4
4. py.plot和py.iplot区别
py.plot会生成一个离线的html文件,里面放置图片。而py.iplot则直接在ipython notebook里面生成图片。
5. 散点图和折线图
基本散点图:
import plotly.offline as py
from plotly.graph_objs import Scatter, Layout
import plotly.graph_objs as gopy.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])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
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])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
6. 设置图例和图例位置
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
为点加上文字:
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
更多设置: plotly.Scatter设置
7. 设置坐标轴
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
更多关于坐标轴设置的参数详见: plotly坐标轴设置
8.Heatmap
trace=go.Heatmap(z=[[1,2,3,4],[5,6,7,8]]
)
data=[trace]
py.iplot(data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
8. Barchart
trace=go.Bar(x=[1,2],y=[1,2],marker=dict(color=["#FF0000", "#00FF00"],)
)
data=[trace]
py.iplot(data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
更多设置:plotly.Bar
9. 填充区域
trace=go.Scatter(x=[1,2,6],y=[1,2,0.5],fill="tonexty",fillcolor="#FF0"
)
data=[trace]
py.iplot(data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
10. 直方图
trace=go.Histogram(x=[1,2,3,3,3,4,5],marker=dict(color=["#F0F","FF0","0FF"])
)
data=[trace]
py.iplot(data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
11. 画地图
trace=go.Scattergeo(lon=[100, 400],lat=[0,0],marker=dict(color=['red', 'blue'],size=[30,50]),mode="markers"
)
data=[trace]
py.iplot(data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
12. 3D表面图
trace=go.Surface(colorscale='Viridis',z=[[3,5,8,13],[21,13,8,5]]
)
data=[trace]
py.iplot(data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
13, 箱型图(Box-plot)
trace=go.Box(x=[1,2,3,3,3,4,5]
)
data=[trace]
py.iplot(data)
- 1
- 2
- 3
- 4
- 5
14. 画地图
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
15. 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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
16. 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)
- 1
- 2
- 3
- 4
- 5
- 6