使用DataFrame绘制的三维散点图



我想使用plotly生成一个3D散点图。我尝试了两种不同的方法来创建图表。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,#将颜色设置为所需值的数组/列表

名称错误:名称"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中没有。

这个问题很老,但对于任何想要答案的人来说:代替(第17行):

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

你应该放:

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

最新更新