如何在Plotly 3D散点图中更改颜色

6
我正在学习如何使用Plotly绘制3D散点图,使用他们的示例并使用自己的数据。示例链接样本在此
我可以绘制出散点图(看起来很酷),但是我无法将不同的数据系列点显示为不同的颜色。
import plotly #load plotly for plotting
import plotly.plotly as py
from plotly import tools
from plotly.graph_objs import * #all the types of plots that we will plot here
plotly.offline.init_notebook_mode() # run at the start of every ipython notebook

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


data = [trace1]
layout = Layout(
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)
fig = Figure(data=data, layout=layout)
plotly.offline.iplot(fig, filename='3d-scatter-colorscale')

我尝试过使用其他示例来自不同的来源,例如Cambridge Spark,但是我没有足够的经验来搞清楚如何使其工作。我相信这是我错过的一个简单问题,但是我找不到它。

颜色需要是一个与x/y/z大小相同的数组/列表。如果您能分享一些数据(或其他数据),我相信我们可以得到一些有效的结果。 - Maximilian Peters
1
嗨 @MaximilianPeters,我已经在上面的Google Drive中添加了我正在使用的示例。 - Nick Duddy
1个回答

8

我应该将我的系列之一作为标记点。在这种情况下,我使用了 "spent" 而不是 x、y、z 的列表。

import plotly #load plotly for plotting
import plotly.plotly as py
from plotly import tools
from plotly.graph_objs import * #all the types of plots that we will plot here
plotly.offline.init_notebook_mode() # run at the start of every ipython notebook

trace1 = Scatter3d(
    x = res,
    y = lc,
    z = spent,
    mode='markers',
    marker=dict(
        size=12,
        color=spent,  # set color to an array/list of desired values
        colorscale='Viridis',   # choose a colorscale
        opacity=0.8
    )
)


data = [trace1]
layout = Layout(
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)
fig = Figure(data=data, layout=layout)
plotly.offline.iplot(fig, filename='3d-scatter-colorscale')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接