如何在Plotly 3D Scatter中更改颜色

时间:2017-06-08 07:34:00

标签: python-3.x plotly

我正在学习如何使用他们的example和我自己的数据在Plotly上绘制3D散点图。 Sample here

我可以得到散点图(看起来很酷),但我不能让不同的数据系列点显示为单独的颜色。

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

我已尝试将其他示例用于单独的来源e.g. Cambridge Spark,但我没有经验来弄清楚如何使其发挥作用。

我确信这是一件很简单的事情,我已经错过但却无法看到它。

1 个答案:

答案 0 :(得分:1)

我应该把我的一个系列作为标记点。在这种情况下,我使用了花而不是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')