作者介绍

莲石东路@乌森

心之所向,无界成长。从底层架构到应用实战,聊聊炼数成金背后的故事。

持续更新数据治理、数据科学、数据可视化、空间计算系列文章。


1

plotly


plotly是一个基于plotly.js构建的可视化工具库,支持2D、3D图形,交互流畅。相较于matplotlib这种基础绘图组件,plotly的可视化展现效果更好,可以媲美Tableau的高质量图。

11

plotly的名词解释




踪迹(trace):类似于matplotlib中的图形(例如折线),只管画图。
布局(layout):规定了一些需要辅助绘制的内容,如标题、图例等。
数据(data):一个踪迹的列表,因为可能会同时绘制多个图形,如多条折线。
画布(figure):与matplotlib中的画布类似,需要传入data和layout参数

12

plotly的绘图步骤




step 1:准备数据
step 2:创建一个或多个踪迹(traces)
step 3:同层踪迹合并
step 4:创建布局(layout)
step 5:在画布(figure)上组合graph部分和layout部分
step 6:图像显示/保存

import plotly as pyimport plotly.graph_objs as goimport numpy as npif __name__ == '__main__':    # 准备数据    x = [i for i in range(60)]    y_shanghai = [np.random.uniform(15, 18) for i in x]    y_beijing = [np.random.uniform(12, 17) for i in x]
# 创建踪迹 trace1 = go.Scatter( x=x, y=y_shanghai, mode='markers', name='shanghai' ) trace2 = go.Scatter( x=x, y=y_beijing, mode='markers', name='beijing' ) # 同层踪迹拼合 data = [trace1, trace2]
# 创建布局 layout = go.Layout(title='示例')
# 在画布(figure)上按照布局绘制踪迹 fig = go.Figure(data=data, layout=layout) # 显示    py.offline.plot(fig, filename='first_offline_start.html', auto_open=True)

plotly官方文档:https://plot.ly/python/

2

多子图绘制功能


同matplotlib一样,plotly的一个画布对象也可以包含多个子图。

import plotly as pyimport plotly.graph_objs as go
trace1 = go.Scatter( x=[1, 2, 3], y=[4, 5, 6])trace2 = go.Scatter( x=[20, 30, 40], y=[50, 60, 70], xaxis='x2', yaxis='y2')trace3 = go.Scatter( x=[300, 400, 500], y=[600, 700, 800], xaxis='x3', yaxis='y3')trace4 = go.Scatter( x=[4000, 5000, 6000], y=[7000, 8000, 9000], xaxis='x4', yaxis='y4')data = [trace1, trace2, trace3, trace4]
layout = go.Layout( xaxis=dict( domain=[0, 0.45] ), yaxis=dict( domain=[0, 0.45] ), xaxis2=dict( domain=[0.55, 1] ), xaxis3=dict( domain=[0, 0.45], anchor='y3' ), xaxis4=dict( domain=[0.55, 1], anchor='y4' ), yaxis2=dict( domain=[0, 0.45], anchor='x2' ), yaxis3=dict( domain=[0.55, 1] ), yaxis4=dict( domain=[0.55, 1], anchor='x4' ))
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='multiple-subplots.html', auto_open=True)

3

绘制二维图表


plotly提供了常见二维图表的绘制。

31

数据分布




1)直方图
import plotly as pyimport plotly.graph_objs as goimport plotly.figure_factory as ffimport numpy as np
x = [np.random.randint(1, 100) for i in range(100)] # 每个数字必须是整数
trace = go.Histogram( x=x)data = [trace]
layout = go.Layout(title='示例')
fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='test.html', auto_open=True)

2)箱型图
import plotly as pyimport plotly.graph_objs as goimport plotly.figure_factory as ffimport numpy as np
y1 = np.random.randn(50)-1y2 = np.random.randn(50)+1 trace1 = go.Box( y=y1)trace2 = go.Box( y=y2)data = [trace1, trace2]
layout = go.Layout(title='示例')
fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='test.html', auto_open=True)

3)小提琴图
import plotly as pyimport plotly.graph_objs as goimport plotly.figure_factory as ffimport numpy as np
y1 = np.random.randn(50)-1y2 = np.random.randn(50)+1 trace1 = go.Violin( y=y1)trace2 = go.Violin( y=y2)data = [trace1, trace2]
layout = go.Layout(title='示例')
fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='test.html', auto_open=True)


32

数据比较




1)柱状图
import plotly as pyimport plotly.graph_objs as goimport numpy as np
trace1 = go.Bar( x = ['Jan','Feb','Mar','Apr', 'May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'], y = [20,14,25,16,18,22,19,15,12,16,14,17], name = 'Primary Product', marker=dict( color = 'rgb(49,130,189)' ))trace2 = go.Bar( x = ['Jan','Feb','Mar','Apr', 'May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'], y = [19,14,22,14,16,19,15,14,10,12,12,16], name = 'Secondary Product', marker=dict( color = 'rgb(204,204,204)' ))data = [trace1, trace2]
layout = go.Layout(title='示例')
fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='test.html', auto_open=True)

2)雷达图
import plotly as pyimport plotly.graph_objs as go
categories = ['processing cost','mechanical properties','chemical stability', 'thermal stability', 'device integration']
trace1 = go.Scatterpolar( r=[1, 5, 2, 2, 3], theta=categories, fill='toself', name='Product A')
trace2 = go.Scatterpolar( r=[4, 3, 2.5, 1, 2], theta=categories, fill='toself', name='Product B')data = [trace1, trace2]
layout = go.Layout(title='示例')
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='test.html', auto_open=True)

3)折线图
import plotly as pyimport plotly.graph_objs as goimport numpy as np
x = [i for i in range(100)]y1 = np.random.randn(100)y2 = np.random.randn(100)
trace1 = go.Scatter( x=x, y=y1, mode='lines', name='line1')trace2 = go.Scatter( x=x, y=y2, mode='lines', name='line2')data = [trace1, trace2]
layout = go.Layout(title='示例')
fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='test.html', auto_open=True)

33

数据关系




1)散点图
import plotly as pyimport plotly.graph_objs as goimport numpy as np
random_x = np.random.randn(1000)random_y = np.random.randn(1000)
trace = go.Scatter( x=random_x, y=random_y, mode='markers')data = [trace]
layout = go.Layout(title='示例')
fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='test.html', auto_open=True)

2)气泡图
import plotly as pyimport plotly.graph_objs as goimport numpy as np
random_x = np.random.randn(1000)random_y = np.random.randn(1000)
trace = go.Scatter( x=random_x, y=random_y, mode='markers', marker=dict( size=16, color=np.random.randn(500), colorscale='Viridis', showscale=True ))data = [trace]
layout = go.Layout(title='示例')
fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='test.html', auto_open=True)

34

数据成分




1)饼图
import plotly as pyimport plotly.graph_objs as goimport plotly.figure_factory as ffimport numpy as np
labels = ['产品1','产品2','产品3','产品4','产品5']values = [38.7,15.33,19.9,8.6,17.47]
trace = go.Pie( labels=labels, values=values, hole=0.3, # 空白区域比例 hoverinfo="label + percent")data = [trace]
layout = go.Layout(title='示例')
fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='test.html', auto_open=True)

2)堆积柱形图
import plotly as pyimport plotly.graph_objs as goimport plotly.figure_factory as ffimport numpy as np
trace1 = go.Bar( y = ['CU.SHF', 'AG.SHF', 'AU.SHF'], x = [21258, 30279, 8056], name = '期货1', orientation = 'h', marker = dict( color = '#104E8B', line = dict( color = '#104E8B', width = 3) ))trace2 = go.Bar( y = ['CU.SHF', 'AG.SHF', 'AU.SHF'], x = [19853, 9375, 4063], name = '期货2', orientation = 'h', marker = dict( color = '#1874CD', line = dict( color = '#104E8B', width = 3) ))trace3 = go.Bar( y = ['CU.SHF', 'AG.SHF', 'AU.SHF'], x = [4959, 13018, 8731], name = '期货3', orientation = 'h', marker = dict( color = '#1C86EE', line = dict( color = '#104E8B', width = 3) ))
data = [trace1, trace2, trace3]
layout = go.Layout( title='稀有金属期货持仓量对比图', barmode='stack')
fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='test.html', auto_open=True)
拓展阅读:https://www.cnblogs.com/ws17345067708/tag/Plotly/

4

数据成分


使用plotly绘制三维图表的方法和绘制二维图表类似,也不需要引入额外模块。


1)三维空间中的点

import numpy as npimport plotly as pyimport plotly.graph_objs as go
x1, y1, z1 = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 200).transpose()trace1 = go.Scatter3d( x=x1, y=y1, z=z1, mode='markers', marker=dict( color='rgb(217, 217, 217)', size=12, symbol='circle', opacity=0.8 ))
x2, y2, z2 = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 200).transpose()trace2 = go.Scatter3d( x=x2, y=y2, z=z2, mode='markers', marker=dict( color='rgb(127, 127, 127)', size=12, symbol='circle', opacity=0.8 ))
data = [trace1, trace2]
layout = go.Layout( title='三维散点', autosize=True, margin=dict( l=65, r=50, b=65, t=90 ))
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='test.html', auto_open=True)


2)三维空间中的线

import numpy as npimport plotly as pyimport plotly.graph_objs as go
def f(x, y): z = np.power(x, 2) + np.power(y, 2) return z x = y = np.linspace(start=0, stop=10, num=1000)z = f(x, y)
trace = go.Scatter3d( x=x, y=y, z=z, mode='lines')
data = [trace]
layout = go.Layout( title='三维线', autosize=True, margin=dict( l=65, r=50, b=65, t=90 ))
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='test.html', auto_open=True)
fig.show()


3)三维空间中的面

import numpy as npimport plotly as pyimport plotly.graph_objs as go
def f(x, y): z = np.power(x, 2) + np.power(y, 2) return z
a = np.linspace(start=0, stop=3, num=4, dtype=np.int32)b = np.linspace(start=0, stop=3, num=4, dtype=np.int32)
x, y = np.meshgrid(a, b)z = f(x, y)

trace = go.Surface(x=x, y=y, z=z)data = [trace]
layout = go.Layout( title='三维曲面', autosize=True, margin=dict( l=65, r=50, b=65, t=90 ))
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='test.html', auto_open=True)   


5

高级封装

Plotly Express是Plotly的高级封装,它为复杂的图表绘制提供了一种简单的语法。


51

数据分布




1)直方图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
tips = px.data.tips()
fig = px.histogram( tips, x="total_bill", y="tip", color="sex", marginal="rug", hover_data=tips.columns)
py.offline.plot(fig, filename='test.html', auto_open=True)


2)箱形图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
tips = px.data.tips()
fig = px.box(tips, x="day", y="total_bill", color="smoker", notched=True)
py.offline.plot(fig, filename='test.html', auto_open=True)   


3)小提琴图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
tips = px.data.tips()
fig = px.violin( tips, y="tip", x="smoker", color="sex", box=True, points="all", hover_data=tips.columns)
py.offline.plot(fig, filename='test.html', auto_open=True)


52

数据比较




1)柱形图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
tips = px.data.tips()
fig = px.bar(tips, x="sex", y="total_bill", color="smoker", barmode="group")
py.offline.plot(fig, filename='test.html', auto_open=True)


2)分组柱形图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
tips = px.data.tips()
fig = px.bar( tips, x="sex", y="total_bill", color="smoker", barmode="group", facet_row="time", facet_col="day", category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
py.offline.plot(fig, filename='test.html', auto_open=True)   


3)雷达图

import pandas as pdimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
df = pd.DataFrame(dict( r=[1, 5, 2, 2, 3], theta=['processing cost','mechanical properties','chemical stability', 'thermal stability', 'device integration']))
fig = px.line_polar(df, r='r', theta='theta', line_close=True)# fig.update_traces(fill='toself')
py.offline.plot(fig, filename='test.html', auto_open=True)


4)折线图

import plotly as pyimport plotly.express as pximport plotly.graph_objs as go
gapminder = px.data.gapminder()
# 选取2个国家:中国、阿富汗country_list = ["China", "Afghanistan"] tem_gapminder = gapminder.query("country == @country_list")
fig = px.line( tem_gapminder, x="year", y="lifeExp", color="country", line_shape="spline", line_dash="country", title="国家人均寿命")# 修改时添加的代码fig.update_traces(dict(mode="markers+lines"))fig.update(layout=dict(xaxis=dict(title="年份", showline=True, nticks=10), yaxis=dict(title="人均寿命", showline=True, nticks=10)))
py.offline.plot(fig, filename='test.html', auto_open=True)


53

数据关系




1)带边缘的散点图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
iris = px.data.iris()
fig = px.scatter( iris, x="sepal_width", y="sepal_length", color="species", marginal_y="violin", marginal_x="box", trendline="ols")
py.offline.plot(fig, filename='test.html', auto_open=True)


2)矩阵散点图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
iris = px.data.iris()
fig = px.scatter_matrix( iris, dimensions=["sepal_width", "sepal_length", "petal_width", "petal_length"], color="species")
py.offline.plot(fig, filename='test.html', auto_open=True)


3)带时间轴的散点图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
gapminder = px.data.gapminder()
fig = px.scatter( gapminder, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country", size="pop", color="continent", hover_name="country", facet_col="continent",log_x=True, size_max=45, range_x=[100,100000], range_y=[25,90])
py.offline.plot(fig, filename='test.html', auto_open=True)


4)三元散点图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
election = px.data.election()
fig = px.scatter_ternary( election, a="Joly", b="Coderre", c="Bergeron", color="winner", size="total", hover_name="district", size_max=15, color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"})
py.offline.plot(fig, filename='test.html', auto_open=True)


5)平行坐标图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
iris = px.data.iris()
fig = px.parallel_coordinates( iris, color="species_id", labels={"species_id": "Species", "sepal_width": "Sepal Width", "sepal_length": "Sepal Length", "petal_width": "Petal Width", "petal_length": "Petal Length"}, color_continuous_scale=px.colors.diverging.Tealrose, color_continuous_midpoint=2)
py.offline.plot(fig, filename='test.html', auto_open=True)


6)并行类别图

import numpy as npimport plotly as pyimport plotly.express as pximport plotly.graph_objs as go
tips = px.data.tips()
fig = px.parallel_categories( tips, color="size", color_continuous_scale=px.colors.sequential.Inferno)
py.offline.plot(fig, filename='test.html', auto_open=True)


54

极坐标




1)极坐标散点图

import plotly as pyimport plotly.express as pximport plotly.graph_objs as go
wind = px.data.wind()
fig = px.scatter_polar(wind, r="frequency", theta="direction")
py.offline.plot(fig, filename='test.html', auto_open=True)


2)极坐标条形图

import plotly as pyimport plotly.express as pximport plotly.graph_objs as go
wind = px.data.wind()
fig = px.scatter_polar(wind, r="frequency", theta="direction")
py.offline.plot(fig, filename='test.html', auto_open=True)


55

地理




1)地理坐标散点图

import plotly as pyimport plotly.express as pximport plotly.graph_objs as go
gapminder = px.data.gapminder()
fig = px.scatter_geo( gapminder, locations="iso_alpha", color="continent", hover_name="country", size="pop", animation_frame="year", projection="natural earth")
py.offline.plot(fig, filename='test.html', auto_open=True)


2)地理区域图

import plotly as pyimport plotly.express as pximport plotly.graph_objs as go
gapminder = px.data.gapminder()
fig = px.choropleth( gapminder, locations="iso_alpha", color="lifeExp", hover_name="country", animation_frame="year",color_continuous_scale=px.colors.sequential.Plasma, range_color=[20,80])
py.offline.plot(fig, filename='test.html', auto_open=True)   


56

箭头图





1)基本的箭头图

import plotly.figure_factory as ffimport numpy as np
x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))u = np.cos(x)*yv = np.sin(x)*y
fig = ff.create_quiver(x, y, u, v)
py.offline.plot(fig, filename='test.html', auto_open=True)


2)带中心点的箭头图

import plotly as pyimport plotly.figure_factory as ffimport plotly.graph_objects as goimport numpy as np
x, y = np.meshgrid(np.arange(-2, 2, .2), np.arange(-2, 2, .25))z = x*np.exp(-x**2 - y**2)v, u = np.gradient(z, .2, .2)
# 创建箭头图fig = ff.create_quiver( x, y, u, v, scale=.25, arrow_scale=.4, name='quiver', line_width=1)
# 往图中添加点fig.add_trace( go.Scatter(x=[-.7, .75], y=[0,0], mode='markers', marker_size=12, name='points'))
py.offline.plot(fig, filename='test.html', auto_open=True)

点赞(1259) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部