繁体   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