# Plotly 3D 散點圖

## 在 Python 中使用 Plotly 的 `scatter_3d()` 函式建立 3D 散點圖

``````import plotly.express as px

df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='species')
fig.show()
``````

`width``height` 引數用於設定上圖的寬度和高度（以畫素為單位）。我們還可以使用資料框建立 3D 散點圖。

`scatter_3d()` 函式將使用顏色序列來設定每個氣泡的顏色。我們可以使用 `size` 引數設定每個氣泡的大小，並將其值設定為整數列表或資料框列。

``````import plotly.express as px

df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='species', size='sepal_width', hover_name='species')
fig.show()
``````

## 在 Python 中更改 3D 散點圖的顏色和軌跡

``````import plotly.express as px

df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='species', size='sepal_width',error_z='petal_width',color_discrete_sequence=px.colors.qualitative.Dark24)
fig.show()
``````

``````import plotly.express as px

df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='species',color_discrete_map={'setosa':'red','versicolor':'green'})
fig.show()
``````

``````import plotly.express as px

df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='species',error_z='petal_width')
fig.update_traces(showlegend=False, error_z_color='red')
fig.show()
``````

Author: Ammar Ali

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.