在 Plotly-Python 中使用 graph_objects 类的 3D 散点图
Plotly是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 plotly 是一个交互式可视化库。
使用 graph_objects 类的散点图
如果 plotly express 没有提供一个好的起点,那么可以使用plotly.graph_objects中的 go.Scatter3D 类。散点图是那些数据点在水平轴和垂直轴上表示的图表,以显示一个变量如何影响另一个变量。属性的模式决定了数据点的外观。
Syntax: plotly.graph_objects.Scatter3d(arg=None, connectgaps=None, customdata=None, customdatasrc=None, error_x=None, error_y=None, error_z=None, hoverinfo=None, hoverinfosrc=None, hoverlabel=None, hovertemplate=None, hovertemplatesrc=None, hovertext=None, hovertextsrc=None, ids=None, idssrc=None, legendgroup=None, line=None, marker=None, meta=None, metasrc=None, mode=None, name=None, opacity=None, projection=None, scene=None, showlegend=None, stream=None, surfaceaxis=None, surfacecolor=None, text=None, textfont=None, textposition=None, textpositionsrc=None, textsrc=None, texttemplate=None, texttemplatesrc=None, uid=None, uirevision=None, visible=None, x=None, xcalendar=None, xsrc=None, y=None, ycalendar=None, ysrc=None, z=None, zcalendar=None, zsrc=None, **kwargs)
Parameters:
x – Sets the x coordinates.
y – Sets the y coordinates.
z – Sets the z coordinates.
mode – Determines the drawing mode for this scatter trace.
示例 1:
Python3
import plotly.express as px
import plotly.graph_objects as go
df = px.data.tips()
fig = go.Figure(data =[go.Scatter3d(x = df['total_bill'],
y = df['time'],
z = df['tip'],
mode ='markers')])
fig.show()
Python3
import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()
fig = go.Figure(data =[go.Scatter3d(x = df['sepal_width'],
y = df['sepal_length'],
z = df['petal_length'],
mode ='markers')])
fig.show()
Python3
import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()
fig = go.Figure(data =[go.Scatter3d(x = df['sepal_width'],
y = df['sepal_length'],
z = df['petal_length'],
mode ='markers',
marker = dict(
size = 12,
color = df['petal_width'],
colorscale ='Viridis',
opacity = 0.8
)
)])
fig.show()
Python3
import plotly.express as px
import plotly.graph_objects as go
df = px.data.tips()
fig = go.Figure(data =[go.Scatter3d(x = df['total_bill'],
y = df['time'],
z = df['day'],
mode ='markers',
marker = dict(
color = df['tip'],
colorscale ='Viridis',
opacity = 0.5
)
)])
fig.show()
输出:
示例 2:
Python3
import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()
fig = go.Figure(data =[go.Scatter3d(x = df['sepal_width'],
y = df['sepal_length'],
z = df['petal_length'],
mode ='markers')])
fig.show()
输出:
使用颜色缩放和标记样式呈现 3D 散点图
在 plotly 中,颜色缩放和标记样式是一种更有效地表示数据的方法,它使数据更易于理解。
示例 1:
Python3
import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()
fig = go.Figure(data =[go.Scatter3d(x = df['sepal_width'],
y = df['sepal_length'],
z = df['petal_length'],
mode ='markers',
marker = dict(
size = 12,
color = df['petal_width'],
colorscale ='Viridis',
opacity = 0.8
)
)])
fig.show()
输出:
示例 2:
Python3
import plotly.express as px
import plotly.graph_objects as go
df = px.data.tips()
fig = go.Figure(data =[go.Scatter3d(x = df['total_bill'],
y = df['time'],
z = df['day'],
mode ='markers',
marker = dict(
color = df['tip'],
colorscale ='Viridis',
opacity = 0.5
)
)])
fig.show()
输出: