Plotly Express 简介
Plotly Express是Plotly的一个易于使用的高级接口,它对各种类型的数据进行操作并生成易于设置样式的图形。
相比而言,Plotly Express使用效率更高,定制化参数较少。权衡学习成本,分享接下来更侧重于使用通用模块Graph_objects。
仅就Plotly Express举一个小例子:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
size='petal_length', hover_data=['petal_width'])
fig.show()
为了提高学习效率,后面的分享将忽略express部分的介绍
graph objects
2.1 基本的点图
与Express接口相比,graph_objects通用端口通过mode参数来区分点图('mode=markers')、线图('mode=lines',默认)
import plotly.graph_objects as go
import numpy as np
N = 1000
t = np.linspace(0, 10, 100)
y = np.sin(t)
fig = go.Figure(data=go.Scatter(x=t, y=y, mode='markers'))
fig.show()
下面通过numpy产生随机数据,展示了mode控制的点图和线图,以及点线图混合模式:
需要说明的是,下面代码运用了先实例化go.Figure()对象,再通过add_trace()方法添加图形信息
import plotly.graph_objects as go
# Create random data with numpy
import numpy as np
np.random.seed(1)
N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N) + 5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 5
fig = go.Figure()
# Add traces
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
mode='markers',
name='markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y1,
mode='lines+markers',
name='lines+markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y2,
mode='lines',
name='lines'))
fig.show()
2.2 气泡图
气泡图是点图的一个重要延伸,通过对气泡大小的控制,实现对第三个维度数据的体现(一般通过点的大小)。
通过颜色的渐变定义,更可以通过颜色展示第四个维度的数据,后面会有例子
气泡图的一个简单例子:
import plotly.graph_objects as go
fig = go.Figure(data=go.Scatter(
x=[1, 2, 3, 4],
y=[10, 11, 12, 13],
mode='markers',
marker=dict(size=[40, 60, 80, 100],
color=[0, 1, 2, 3])
))
fig.show()
上述例子中在mode指定markers后,marker的核心参数(需传入字典)为:
- size 大小
- color 颜色,亦可以用'rgb(93, 164, 214)'表示;
- opacity 不透明度,0-1.0,其中1.0为完全不透明;
import plotly.graph_objects as go
fig = go.Figure(data=[go.Scatter(
x=[1, 2, 3, 4], y=[10, 11, 12, 13],
mode='markers',
marker=dict(
color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)',
'rgb(44, 160, 101)', 'rgb(255, 65, 54)'],
opacity=[1, 0.8, 0.6, 0.4],
size=[40, 60, 80, 100],
)
)])
fig.show()
若要缩放气泡大小,可以使用属性sizeref,sizeref如果设置为小于1,则会增加渲染标记的大小;反之sizeref如果设置为大于1,则会减少渲染标记的大小。
为了显示效果适宜,官方推荐sizeref参数使用:2. * size中实际的最大值 / (预期的size最大值大小 ** 2)
进行指定。
import plotly.graph_objects as go
size = [20, 40, 60, 80, 100, 80, 60, 40, 20, 40]
fig = go.Figure(data=[go.Scatter(
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y=[11, 12, 10, 11, 12, 11, 12, 13, 12, 11],
mode='markers',
marker=dict(
size=size,
sizemode='area',
sizeref=2.*max(size)/(40.**2),
sizemin=4
)
)])
fig.show()
这里特别关注,必须设置 sizemode='area' 以上配置规律才能展示预期效果,否则会出现奇怪的结果。
sizemin参数顾名思义,与sizeref配套使用,对气泡的大小范围进行控制。
与其他图形类似,在图形中可以加入text参数调整鼠标移动至图形上时显示的内容:
import plotly.graph_objects as go
fig = go.Figure(data=[go.Scatter(
x=[1, 2, 3, 4], y=[10, 11, 12, 13],
text=['A
size: 40', 'B
size: 60', 'C
size: 80', 'D
size: 100'],
mode='markers',
marker=dict(
color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)', 'rgb(44, 160, 101)', 'rgb(255, 65, 54)'],
size=[40, 60, 80, 100],
)
)])
fig.show()
通过加入showscale
显示右边颜色指示条
但是颜色的变化必须是渐变的,否则颜色指示条会显示异常
import plotly.graph_objects as go
fig = go.Figure(data=[go.Scatter(
x=[1, 3.2, 5.4, 7.6, 9.8, 12.5],
y=[1, 3.2, 5.4, 7.6, 9.8, 12.5],
mode='markers',
marker=dict(
color=[120, 125, 130, 135, 140, 145],
size=[15, 30, 55, 70, 90, 110],
showscale=True
)
)])
fig.show()
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import math
# Load data, define hover text and bubble size
data = px.data.gapminder()
df_2007 = data[data['year']==2007]
df_2007 = df_2007.sort_values(['continent', 'country'])
hover_text = []
bubble_size = []
for index, row in df_2007.iterrows():
hover_text.append(('Country: {country}
'+
'Life Expectancy: {lifeExp}
'+
'GDP per capita: {gdp}
'+
'Population: {pop}
'+
'Year: {year}').format(country=row['country'],
lifeExp=row['lifeExp'],
gdp=row['gdpPercap'],
pop=row['pop'],
year=row['year']))
bubble_size.append(math.sqrt(row['pop']))
df_2007['text'] = hover_text
df_2007['size'] = bubble_size
sizeref = 2.*max(df_2007['size'])/(100**2)
# Dictionary with dataframes for each continent
continent_names = ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
continent_data = {continent:df_2007.query("continent == '%s'" %continent)
for continent in continent_names}
# Create figure
fig = go.Figure()
for continent_name, continent in continent_data.items():
fig.add_trace(go.Scatter(
x=continent['gdpPercap'], y=continent['lifeExp'],
name=continent_name, text=continent['text'],
marker_size=continent['size'],
))
# Tune marker appearance and layout
fig.update_traces(mode='markers', marker=dict(sizemode='area',
sizeref=sizeref, line_width=2))
fig.update_layout(
title='Life Expectancy v. Per Capita GDP, 2007',
xaxis=dict(
title='GDP per capita (2000 dollars)',
gridcolor='white',
type='log',
gridwidth=2,
),
yaxis=dict(
title='Life Expectancy (years)',
gridcolor='white',
gridwidth=2,
),
paper_bgcolor='rgb(243, 243, 243)',
plot_bgcolor='rgb(243, 243, 243)',
)
data.head()
fig.show()
官方给出的高阶气泡图实例
- 一次遍历,生成所需的'text'和'size'字段
- 二次遍历,针对每个大洲的数据进行绘制信息添加
- 添加通用的绘制信息
- 对绘制的样式进行调整
其中对整体面板的调整:'paper_bgcolor', 'plot_bgcolor' 背景色的设置;
其中对横竖坐标轴样式的调整:'gridcolor','gridwidth','type' 颜色、宽度和样式
2.3 点图的高阶样式
import plotly.graph_objects as go
import numpy as np
t = np.linspace(0, 10, 100)
fig = go.Figure()
fig.add_trace(go.Scatter(
x=t, y=np.sin(t),
name='sin',
mode='markers',
marker_color='rgba(152, 0, 0, .8)'
))
fig.add_trace(go.Scatter(
x=t, y=np.cos(t),
name='cos',
marker_color='rgba(255, 182, 193, .9)'
))
# Set options common to all traces with fig.update_traces
fig.update_traces(mode='markers', marker_line_width=2, marker_size=10,
marker_opacity=0.9)
fig.update_layout(title='Styled Scatter',
yaxis_zeroline=False, xaxis_zeroline=False)
fig.show()
可以看出,marker_size/color/opacity=[]
与marker=dict(size=[],color=[],opacity=[])
在效果上是等价的。
下面的例子用来特别展示 text
参数的结果,和 marker_showscale
颜色标尺的显示
import plotly.graph_objects as go
import numpy as np
num = np.random.randn(500)
fig = go.Figure(data=go.Scatter(
y = num,
mode='markers',
marker=dict(
size=16,
color=np.random.randn(500), #set color equal to a variable
colorscale='Viridis', # one of plotly colorscales
showscale=True, # 显示颜色标尺
),
text=[f"Value: {i}" for i in num], # 显示文本内容
))
fig.show()
针对大数据量,可以用Scattergl()代替Scatter()来实现WebGL为了提高速度,提高交互性,以及绘制更多数据的能力!
import plotly.graph_objects as go
import numpy as np
N = 100000
r = np.random.uniform(0, 1, N)
theta = np.random.uniform(0, 2*np.pi, N)
fig = go.Figure(data=go.Scattergl(
x = r * np.cos(theta), # non-uniform distribution
y = r * np.sin(theta), # zoom to see more points at the center
mode='markers',
marker=dict(
color=np.random.randn(N),
colorscale='Viridis',
line_width=1
)
))
fig.show()