【问题标题】:3D Scatter Plot in Plotly using DataFrame使用 DataFrame 在 Plotly 中绘制 3D 散点图
【发布时间】:2019-07-02 19:41:04
【问题描述】:

我想使用 plotly 生成 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()

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

接下来的代码提供了“NameError: name 'z' is not defined”。从数据帧创建 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 散点图,但不能在 plotly 中生成。

【问题讨论】:

  • 如何生成数据框?
  • 数据框是使用 MDS 转换创建的。我会将数据框代码添加到我的问题中。我能够使用数据框在 pyplot 中创建 3D 散点图,但不能在 plotly 中创建。
  • 您之前是否定义了变量z?它似乎不是一个 Plotly,而只是一个未定义的变量。

标签: python plotly


【解决方案1】:

相当老的问题,但对于任何想要答案的人: 而不是(第 17 行):

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

你应该把:

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

【讨论】:

    猜你喜欢
    • 2021-05-16
    • 1970-01-01
    • 2014-09-03
    • 2017-04-29
    • 2021-05-14
    • 2021-04-07
    • 1970-01-01
    • 2020-10-19
    • 2023-02-10
    相关资源
    最近更新 更多