📜  在 Plotly-Python 中使用 graph_objects 类的 3D 散点图

📅  最后修改于: 2022-05-13 01:55:09.467000             🧑  作者: Mango

在 Plotly-Python 中使用 graph_objects 类的 3D 散点图

Plotly是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 plotly 是一个交互式可视化库。

使用 graph_objects 类的散点图

如果 plotly express 没有提供一个好的起点,那么可以使用plotly.graph_objects中的 go.Scatter3D 类。散点图是那些数据点在水平轴和垂直轴上表示的图表,以显示一个变量如何影响另一个变量。属性的模式决定了数据点的外观。

示例 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()

输出: