簡體   English   中英

使用DataFrame進行3D散點圖繪制

[英]3D Scatter Plot in Plotly using DataFrame

我想使用plotly生成3D Scatter圖表。 我嘗試了兩種不同的方法來創建圖表。 Netither已經成功。 第一個代碼不執行任何操作。 這是他們參考中使用的方法。 沒有回溯錯誤。 沒有顯示或生成圖表。

import plotly.plotly as py
import plotly.graph_objs as go
import cufflinks as cf
import plotly
from plotly.offline import download_plotlyjs, init_notebook_mode, plot,iplot
plotly.offline.init_notebook_mode()
init_notebook_mode()
cf.go_offline()

df.iplot(kind='scatter', mode='markers', x='x', y='y', z='label')

接下來的代碼提供了“ NameError:名稱'z'未定義”。 從數據幀創建3D散點圖有沒有技巧或竅門?

import plotly.plotly as py
import plotly.graph_objs as go
import cufflinks as cf
import plotly
from plotly.offline import download_plotlyjs, init_notebook_mode, plot,iplot
plotly.offline.init_notebook_mode()
init_notebook_mode()
cf.go_offline()

trace1 = go.Scatter3d(
    x=df['x'],
    y=df['y'],
    z=df['label'],
    mode='markers',
    marker=dict(
        size=12,
        color=z,                # set color to an array/list of desired values
        colorscale='Viridis',   # choose a colorscale
        opacity=0.8
    )
)



data = [trace1]
layout = go.Layout(
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='DF')

追溯:

文件“”,第8行,在color = z中,#將顏色設置為所需值的數組/列表

NameError:未定義名稱“ z”

該數據幀是使用以下代碼生成的:

dist = 1 - cosine_similarity(vcx)
MDS()
mds = MDS(n_components=k, dissimilarity="precomputed", random_state=1)
pos = mds.fit_transform(dist)  # shape (n_components, n_samples)
xs, ys = pos[:, 0], pos[:, 1]
df = pd.DataFrame(dict(x=xs, y=ys, label=clusters, title=titles)) 

我已經能夠使用pyplot中的數據框生成3D散點圖,但無法通過圖繪制。

相當老的問題,但是對於任何想要答案的人:而不是(第17行):

        color=z,                         # set color to an array/list of desired values

您應該輸入:

        color=df['label'],                # set color to an array/list of desired values

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM