简体   繁体   English

使用DataFrame进行3D散点图绘制

[英]3D Scatter Plot in Plotly using DataFrame

I would like to generate a 3D Scatter chart using plotly. 我想使用plotly生成3D Scatter图表。 I've tried two different methods for creating the chart. 我尝试了两种不同的方法来创建图表。 Netither one has been successful. Netither已经成功。 The first code does not do anything. 第一个代码不执行任何操作。 This was the method that plotly usings in their reference. 这是他们参考中使用的方法。 There is not a traceback error. 没有回溯错误。 No chart is shown or generated. 没有显示或生成图表。

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')

This next code provides a "NameError: name 'z' is not defined". 接下来的代码提供了“ NameError:名称'z'未定义”。 Are there any tricks or tips to creating a 3D scatter from a dataframe? 从数据帧创建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')

Trace Back: 追溯:

File "", line 8, in color=z, # set color to an array/list of desired values 文件“”,第8行,在color = z中,#将颜色设置为所需值的数组/列表

NameError: name 'z' is not defined NameError:未定义名称“ z”

The dataframe was generated using the following code: 该数据帧是使用以下代码生成的:

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)) 

I have been able to generate a 3D scatter using the dataframe in pyplot but not in plotly. 我已经能够使用pyplot中的数据框生成3D散点图,但无法通过图绘制。

Quite old question, but for anyone who want the answer : Instead of (line 17): 相当老的问题,但是对于任何想要答案的人:而不是(第17行):

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

You should put : 您应该输入:

        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