plotlyPython包基于构建的开源库plotly.js(buildond3.js).我们使用的装饰器称为cufflinksPandas,可以使用pandas数据框。因此,我们的整个技术栈顺序都是cufflinks>plotly>plotly.js>d3.js,这意味着我们即通过d3获得令人难以置信的交互式图形功能,同时拥有了Python编码的效率。
线上运行以及绘图效果,欢迎移步 和鲸社区
#terminal里
pip install plotly
http://www.plot.ly/python/getting-started
import plotly.offline as py
import plotly.graph_objs as go
trace = {'x':[1,2], 'y':[1,2]}
data = [trace]
layout = {}
fig = go.Figure(
data = data, layout = layout)
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)
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)
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
Plot.ly是一个用于做分析和可视化的在线工具,Plotly与pandas可以无缝地集成,可以做出很多非常丰富,互动的图表,并且文档非常健全,创建条形图相对简单,另外申请了API密钥后,可以一键将统计图形同步到云端。
下面是一个用Ploty画直方图的例子,我参考的是这篇文章http://www.dcharm.com/?p=599
代码如下
import plotly.plotly as pyimport pandas as pd
from plotly.graph_objs import *
from plotly.offline import plot
budget=pd.read_csv("mn-budget-detail-2014.csv")
budget = budget.sort('amount',ascending=False)[:10]
data = Data([
Bar(
x=budget["detail"],
y=budget["amount"]
)
])
layout = Layout(
title='2014 MN Capital Budget',
font=Font(
family='Raleway, sans-serif'
),
showlegend=False,
xaxis=XAxis(
tickangle=-45
),
bargap=0.05
)
fig = Figure(data=data, layout=layout)
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)