線上運行以及繪圖效果,歡迎移步和鯨社區(qū)
基本步驟
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,? #網(wǎng)格
? ? 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)
數(shù)據(jù)類圖表
直方圖
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])
分級統(tǒng)計地圖
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)
數(shù)據(jù)結構
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 數(shù)字
參考資料:
轉(zhuǎn)載本文請聯(lián)系和鯨取得授權,和鯨社區(qū)是聚合數(shù)據(jù)人才和行業(yè)問題的在線社區(qū),率先打造國內(nèi)首款K-Lab 在線數(shù)據(jù)分析協(xié)作平臺,為數(shù)據(jù)工作者的學習與工作帶來全新的體驗。