# Plotly教程（2）-Plotly玩转散点图

2021/10/13 22:00

### 可视化神器Plotly玩转散点图

• plotly_express，简写为px
• plotly.graph_objects，简写为go

<!--MORE-->

### 导入库

import pandas as pd
import numpy as np
import plotly_express as px
import plotly.graph_objects as go


### 基础散点图

#### 自定义数据

fig = px.scatter(x=[0,2,4,6],
y=[1,3,5,7]
)
fig.show()


#### 传入DataFrame型数据

fig = px.scatter(df,  # 数据集
x="sepal_width",  # x轴
y="sepal_length"  # y轴
)
fig.show()


1、指定颜色

fig = px.scatter(df,  # 数据集
x="sepal_width",  # x轴
y="sepal_length",  # y轴
color="sepal_length"  # 指定颜色
)
fig.show()


2、指定大小

 fig = px.scatter(df,  # 数据集
x="sepal_width",  # x轴
y="sepal_length",  # y轴
color="species",  # 指定颜色
size="sepal_length"  # 指定散点大小
)
fig.show()


3、同时指定颜色和大小

gap = px.data.gapminder().query("year == 2002")

fig = px.scatter(gap   # 绘图DataFrame数据集
,x="gdpPercap"  # 横坐标
,y="lifeExp"  # 纵坐标
,color="continent"  # 区分颜色
,size="pop"   # 区分圆的大小
,size_max=60  # 散点大小
)
fig.show()


### 散点图显示数据

x_data = [0,2,4,6,8]
y_data = [1,3,5,7,9]

fig = px.scatter(x=x_data,
y=y_data,
text=x_data  # 设置显示的文本内容
)
fig.update_traces(textposition="top center")  # 文本显示的位置：顶部居中

fig.show()


### 绘制线型图

df = px.data.carshare()

fig = px.line(df,
x='centroid_lat',
y='car_hours',
color='peak_hour'
)
fig.show()


gapm = px.data.gapminder().query("continent == 'Oceania'")

fig = px.line(gapm,
x='year',
y='lifeExp',
color='country')
fig.show()


### 基于go.Scatter绘制散点图

#### 基础散点图

import plotly.graph_objects as go
import numpy as np

N = 1000
x = np.linspace(0, 20, 100)
y = np.cos(x)

fig = go.Figure(data=go.Scatter(
x=x,
y=y,
mode='markers'))

fig.show()


#### 多个散点图

import plotly.graph_objects as go

import numpy as np
np.random.seed(1)

# 生成随机数据
N = 200
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N) + 10
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 10

# 准备画布
fig = go.Figure()

# 添加3组不同的数据
x=random_x,
y=random_y0,
mode='lines', # mode模式选择
name='lines')) # 名字

x=random_x,
y=random_y1,
mode='lines+markers',
name='lines+markers'))

x=random_x,
y=random_y2,
mode='markers',
name='markers'))

fig.show()


#### 冒泡散点图

fig = go.Figure(data=go.Scatter(
x=[1,3,5,7],
y=[12,18,24,6],
mode='markers',
marker=dict(size=[20,40,60,80],  # marker是字典形式的数据
color=[0,1,2,3]
)
))

fig.show()


#### 自定义散点颜色

import plotly.graph_objects as go
import numpy as np

# 生成x轴数据
t = np.linspace(0, 10, 200)

# 设置画布
fig = go.Figure()

x=t,  # xy轴
y=np.cos(t),
name='cos',  # 名字
mode='markers',  # 采取显示的形状：markers、lines、markers+lines
marker_color='rgba(200, 100, 1, 100.8)'  # 通过rgb设置颜色
))

x=t,
y=np.sin(t),
name='sin',
marker_color='rgba(255, 2, 130, .9)'
))

fig.update_traces(mode='markers',
marker_line_width=2, # 标记外围线宽
marker_size=10)  # 标记大小

fig.update_layout(title='自定义散点图',   # 图形名称
yaxis_zeroline=False,
xaxis_zeroline=False)

fig.show()


#### 设置渐变颜色

import plotly.graph_objects as go
import numpy as np

fig = go.Figure(data=go.Scatter(
y = np.random.randn(400),
mode='markers',
marker=dict(
size=16,
color=np.random.randn(400),
colorscale='sunsetdark',  # matter picnic sunsetdark thermal
showscale=True
)
))

fig.show()


### 大量数据散点图

1、案例1

import numpy as np

N = 100000

fig = go.Figure(data=go.Scattergl(
x = np.random.randn(N),   # 随机生成100000个数字
y = np.random.randn(N),
mode='markers',
marker=dict(
color=np.random.randn(N),  # 随机生成100000个颜色
colorscale='plotly3',
line_width=1
)
))

fig.show()


2、案例2

import plotly.graph_objects as go
import numpy as np

N = 100000
r = np.random.uniform(0, 1, N)  # 随机生成0-1之间100000的一个浮点数
theta = np.random.uniform(0, 2*np.pi, N)  # 随机生成0-2*pi之间的100000个浮点数

fig = go.Figure(data=go.Scattergl(
x = r * np.cos(theta),
y = r * np.sin(theta),
mode='markers',
marker=dict(
color=np.random.randn(N),
colorscale='magma',
line_width=1
)
))

fig.show()


random.uniform()函数的用法：

### 3D散点图

• 基于px的3D散点图
• 基于go的3D散点图

#### 基于plotly_express

1、案例1

import plotly.express as px

df1 = px.data.iris()

fig1 = px.scatter_3d(df1,  # 指定数据
x='sepal_length',  # 指定3个轴
y='sepal_width',
z='petal_width',
color='species')  # 指定颜色
fig1.show()


2、案例2

import plotly.express as px

df2 = px.data.iris()
fig2 = px.scatter_3d(df2,
x='sepal_length',
y='sepal_width',
z='petal_width',
color='petal_length',
symbol='species')
fig2.show()


#### 基于go.Figure

1、案例1

import plotly.graph_objects as go
import numpy as np

t = np.linspace(0, 10, 30)   # 0-10之间随机生成50个数字
x, y, z = np.cos(t), np.sin(t), t   # 设置3个变量

fig = go.Figure(data=[go.Scatter3d(x=x,
y=y,
z=z,
mode='markers')])
fig.show()


2、案例2

import plotly.graph_objects as go
import numpy as np

t = np.linspace(0, 20, 100)
x, y, z = np.cos(t), np.sin(t), t

fig = go.Figure(data=[go.Scatter3d(
x=x,
y=y,
z=z,
mode='markers',
marker=dict(  # 标记设置
size=12,
color=z,
colorscale='piyg',   # 渐变色选择
opacity=0.8  # 透明度设置
)
)])

fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
fig.show()


0
0 收藏

0 评论
0 收藏
0