一、3D散點(diǎn)圖語(yǔ)法
plotly.express.scatter_3d(data_frame=None,
x=None,
y=None,
z=None,
color=None,
symbol=None,
size=None,
text=None,
hover_name=None,
hover_data=None,
custom_data=None,
error_x=None,
error_x_minus=None,
error_y=None,
error_y_minus=None,
error_z=None,
error_z_minus=None,
animation_frame=None,
animation_group=None,
category_orders=None,
labels=None,
size_max=None,
color_discrete_sequence=None,
color_discrete_map=None,
color_continuous_scale=None,
range_color=None,
color_continuous_midpoint=None,
symbol_sequence=None,
symbol_map=None,
opacity=None,
log_x=False,
log_y=False,
log_z=False,
range_x=None,
range_y=None,
range_z=None,
title=None,
template=None,
width=None,
height=None)
二、3D散點(diǎn)圖參數(shù)
-
data_frame ( DataFrame or array-like or dict ) – 這個(gè)參數(shù)需要傳遞給要使用的列名(而不是關(guān)鍵字名)。Array-like 和 dict 在內(nèi)部轉(zhuǎn)換為 Pandas DataFrame??蛇x:如果丟失,則使用其他參數(shù)在幕后構(gòu)造一個(gè) DataFrame。
-
x ( str or int or Series or array-like ) – 中列的名稱data_frame,或pandas Series或array_like對(duì)象。來(lái)自此列或 array_like 的值用于在笛卡爾坐標(biāo)中沿 x 軸定位標(biāo)記。
-
y ( str or int or Series or array-like ) – 中列的名稱data_frame,或pandas Series或array_like對(duì)象。來(lái)自此列或 array_like 的值用于在笛卡爾坐標(biāo)中沿 y 軸定位標(biāo)記。
-
z ( str or int or Series or array-like ) – 中列的名稱data_frame,或pandas Series或array_like對(duì)象。來(lái)自此列或 array_like 的值用于在笛卡爾坐標(biāo)中沿 z 軸定位標(biāo)記。
-
color ( str or int or Series or array-like ) – 中列的名稱data_frame,或pandas Series或array_like對(duì)象。此列或array_like 中的值用于為標(biāo)記分配顏色。
-
symbol 符號(hào)(str或int或Series或類似數(shù)組)– 中列的名稱,data_frame或 Pandas Series 或 array_like 對(duì)象。此列或array_like 中的值用于將符號(hào)分配給標(biāo)記。
-
size ( str or int or Series or array-like ) – 中列的名稱data_frame,或pandas Series或array_like對(duì)象。此列或array_like 中的值用于分配標(biāo)記大小。
-
text ( str or int or Series or array-like ) – 中列的名稱data_frame,或pandas Series或array_like對(duì)象。此列或array_like 中的值在圖中顯示為文本標(biāo)簽。
-
hover_name ( str or int or Series or array-like ) – 中列的名稱data_frame,或者pandas Series或array_like對(duì)象。此列或類似數(shù)組的值在懸停工具提示中以粗體顯示。
-
hover_data 懸停數(shù)據(jù)(str或int列表,或Series或類似數(shù)組,或dict)–data_frame或 pandas Series 中的列名稱列表,或類似數(shù)組的對(duì)象或以列名作為鍵的 dict,值為 True(默認(rèn)情況下)格式) False(為了從懸停信息中刪除此列),或格式字符串,例如 ‘:.3f’ 或 ’ | %a’ 或類似列表的數(shù)據(jù)出現(xiàn)在懸停工具提示或元組中,bool 或格式化字符串作為第一個(gè)元素,類似列表的數(shù)據(jù)出現(xiàn)在懸停中作為第二個(gè)元素這些列中的值在懸停工具提示中顯示為額外數(shù)據(jù).
-
custom_data(str或int列表,或Series或array-like)– 中列的名稱data_frame,或 Pandas Series,或 array_like 對(duì)象這些列中的值是額外的數(shù)據(jù),例如用于小部件或 Dash 回調(diào)。此數(shù)據(jù)不是用戶可見(jiàn)的,但包含在圖形發(fā)出的事件中(套索選擇等)
-
error_x ( str or int or Series or array-like ) – 中列的名稱data_frame,或pandas Series或array_like對(duì)象。此列或 array_like 中的值用于確定 x 軸誤差線的大小。如果error_x_minus是None,誤差線將是對(duì)稱的,否則error_x僅用于正方向。
-
error_x_minus ( str or int or Series or array-like ) – 中列的名稱data_frame,或pandas Series或array_like對(duì)象。此列或 array_like 中的值用于確定負(fù)方向上的 x 軸誤差條的大小。如果error_x 是 則忽略None。
-
error_y ( str or int or Series or array-like ) – 中列的名稱data_frame,或pandas Series或array_like對(duì)象。此列或 array_like 中的值用于確定 y 軸誤差線的大小。如果error_y_minus是None,誤差線將是對(duì)稱的,否則error_y僅用于正方向。
-
error_y_minus ( str or int or Series or array-like ) – 中列的名稱data_frame,或者pandas Series或array_like對(duì)象。此列或 array_like 中的值用于在負(fù)方向上調(diào)整 y 軸誤差條的大小。如果error_y 是 則忽略None。
-
error_z ( str or int or Series or array-like ) – 中的列名data_frame,或者pandas Series或者array_like對(duì)象。此列或 array_like 中的值用于調(diào)整 z 軸誤差線的大小。如果error_z_minus是None,誤差線將是對(duì)稱的,否則error_z僅用于正方向。
-
error_z_minus ( str or int or Series or array-like ) – 中的列名data_frame,或者pandas Series或者array_like對(duì)象。此列或 array_like 中的值用于確定負(fù)方向上的 z 軸誤差條的大小。如果error_z 是 則忽略None。
-
animation_frame(str或int或Series或array-like)– 中列的名稱,data_frame或 Pandas Series 或 array_like 對(duì)象。此列或array_like 中的值用于為動(dòng)畫(huà)幀分配標(biāo)記。
-
animation_group(str或int或Series或array-like)– 中列的名稱,data_frame或 Pandas Series 或 array_like 對(duì)象。來(lái)自此列或 array_like 的值用于提供跨動(dòng)畫(huà)幀的對(duì)象恒定性:具有匹配 “ animation_group”的行將被視為它們?cè)诿恳粠忻枋鱿嗤膶?duì)象。
-
category_orders(帶有 str 鍵的字典和 str 值列表(默認(rèn){}))——默認(rèn)情況下,在 Python 3.6+ 中,軸、圖例和方面中分類值的順序取決于這些值第一次遇到的順序data_frame(并且沒(méi)有3.6 以下的 Python 中默認(rèn)保證順序)。此參數(shù)用于強(qiáng)制對(duì)每列的值進(jìn)行特定排序。這個(gè)字典的鍵應(yīng)該對(duì)應(yīng)于列名,值應(yīng)該是與所需的特定顯示順序?qū)?yīng)的字符串列表。
-
labels 標(biāo)簽(帶有 str 鍵和 str 值的字典(默認(rèn){}))——默認(rèn)情況下,列名在圖中用于軸標(biāo)題、圖例條目和懸停。這個(gè)參數(shù)允許它被覆蓋。此字典的鍵應(yīng)對(duì)應(yīng)于列名,值應(yīng)對(duì)應(yīng)于要顯示的所需標(biāo)簽。
-
size_max (int (default 20)) – 使用時(shí)設(shè)置最大標(biāo)記大小size。
-
color_discrete_sequence(str 列表)——字符串應(yīng)該定義有效的 CSS 顏色。當(dāng)color設(shè)置為 并且相應(yīng)列中的值不是數(shù)字時(shí),該列中的值通過(guò)color_discrete_sequence 按 中描述的順序循環(huán)分配顏色category_orders,除非 的值 color是 中的鍵color_discrete_map。plotly.express.colors子模塊中提供了各種有用的顏色序列,特別是plotly.express.colors.qualitative.
-
color_discrete_map(帶有 str 鍵和 str 值的字典(默認(rèn){}))——字符串值應(yīng)該定義有效的 CSS-colors 用于覆蓋 color_discrete_sequence以將特定顏色分配給與特定值對(duì)應(yīng)的標(biāo)記。鍵入的color_discrete_map應(yīng)該是由 表示的列中的值color。或者,如果 的值color是有效顏色,則’identity’可以傳遞字符串以直接使用它們。
-
color_continuous_scale ( str 列表) – 字符串應(yīng)該定義有效的 CSS-colors 當(dāng) 由 表示的列color包含數(shù)字?jǐn)?shù)據(jù)時(shí),此列表用于構(gòu)建連續(xù)的顏色比例。plotly.express.colors子模塊中提供了各種有用的色階 ,特別 plotly.express.colors.sequential是plotly.express.colors.diverging 和plotly.express.colors.cyclical。
-
range_color(兩個(gè)數(shù)字的列表)– 如果提供,則覆蓋連續(xù)色標(biāo)上的自動(dòng)縮放。
-
color_continuous_midpoint (number (default None)) – 如果設(shè)置,計(jì)算連續(xù)色標(biāo)的邊界以獲得所需的中點(diǎn)。使用plotly.express.colors.diverging色階作為 的輸入時(shí), 建議設(shè)置此值 color_continuous_scale。
-
symbol_sequence(str 列表)——字符串應(yīng)該定義有效的 plotly.js 符號(hào)。當(dāng)symbol被設(shè)置時(shí),該列中的值由通過(guò)循環(huán)分配的符號(hào) symbol_sequence中的順序描述category_orders,除非的值symbol是在一個(gè)關(guān)鍵symbol_map。
-
symbol_map(帶有 str 鍵和 str 值的字典(默認(rèn){}))——字符串值應(yīng)該定義 plotly.js 符號(hào) 用于覆蓋symbol_sequence以將特定符號(hào)分配給與 特定值對(duì)應(yīng)的標(biāo)記。鍵入的symbol_map應(yīng)該是由 表示的列中的值symbol。或者,如果 的值symbol 是有效的符號(hào)名稱,則’identity’可以傳遞字符串以直接使用它們。
-
opacity ( float ) – 介于 0 和 1 之間的值。設(shè)置標(biāo)記的不透明度。
-
log_x (boolean (default False)) – 如果True,x 軸在笛卡爾坐標(biāo)中對(duì)數(shù)縮放。
-
log_y (boolean (default False)) – 如果True,y 軸在笛卡爾坐標(biāo)中對(duì)數(shù)縮放。
-
log_z (boolean (default False)) – 如果True,z 軸在笛卡爾坐標(biāo)中對(duì)數(shù)縮放。
-
range_x(兩個(gè)數(shù)字的列表)– 如果提供,則覆蓋笛卡爾坐標(biāo)中 x 軸上的自動(dòng)縮放。
-
range_y(兩個(gè)數(shù)字的列表)– 如果提供,則覆蓋笛卡爾坐標(biāo)中 y 軸上的自動(dòng)縮放。
-
range_z(兩個(gè)數(shù)字的列表)– 如果提供,則覆蓋笛卡爾坐標(biāo)中 z 軸上的自動(dòng)縮放。
-
title ( str ) – 圖形標(biāo)題。
-
template 模板(str或dict或plotly.graph_objects.layout.Template 實(shí)例)——圖形模板名稱(必須是 plotly.io.templates 中的一個(gè)鍵)或定義。
-
width (int (default None)) – 以像素為單位的圖形寬度。
-
height (int (default None)) – 以像素為單位的圖形高度。
三、返回類型
plotly.graph_objects.Figure
四、實(shí)例
4.1 在三維空間中繪制單個(gè)數(shù)據(jù)
由于標(biāo)記的顏色,可以表示數(shù)據(jù)的第 4 維
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()
4.2 使用列中的值將符號(hào)分配給標(biāo)記
import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='petal_length', symbol='species')
fig.show()
4.3 3d 散點(diǎn)圖樣式
可以通過(guò)px.scatter_3d某些選項(xiàng)的參數(shù)來(lái)自定義圖形的樣式,或者通過(guò) 更新圖形的軌跡或布局fig.update。
import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='petal_length', size='petal_length', size_max=18,
symbol='species', opacity=0.7)
# tight layout
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
4.4 Dash 中的 3d 散點(diǎn)圖
Dash是使用 Plotly 圖形在 Python 中構(gòu)建分析應(yīng)用程序的最佳方式。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-444281.html
import dash
from dash import dcc,html
from dash.dependencies import Input, Output
import plotly.express as px
df = px.data.iris()
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Graph(id="scatter-plot"),
html.P("Petal Width:"),
dcc.RangeSlider(
id='range-slider',
min=0, max=2.5, step=0.1,
marks={0: '0', 2.5: '2.5'},
value=[0.5, 2]
),
])
@app.callback(
Output("scatter-plot", "figure"),
[Input("range-slider", "value")])
def update_bar_chart(slider_range):
low, high = slider_range
mask = (df.petal_width > low) & (df.petal_width < high)
fig = px.scatter_3d(df[mask],
x='sepal_length', y='sepal_width', z='petal_width',
color="species", hover_data=['petal_width'])
return fig
app.run_server(debug=True)
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-444281.html
到了這里,關(guān)于【Python百日進(jìn)階-數(shù)據(jù)分析】Day325 - plotly.express.scatter_3d():3D散點(diǎn)圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!