Skip to content

Unable to define opacity of markers for Scatter3D using rgba #1416

Closed
@blondegeek

Description

@blondegeek

I originally posted this in the plotly community forums and it was recommend I instead post this as an issue.

I would like to define the opacity per point for a Scatter3D plot. I’ve tried to do this by defining a rgba color per point, but the alpha component seems to be treated as white (example code below). Any suggestions? Thanks!

import plotly
import plotly.graph_objs as go
import plotly.offline as off
plotly.offline.init_notebook_mode(connected=False)
import numpy as np

x_max = 2
num_bins = 20

linspace = np.linspace(-x_max, x_max, num_bins)
X, Y, Z = np.meshgrid(linspace, linspace, linspace)

gaussian = lambda x: np.exp(-np.sum(np.power(x, 2), 0, keepdims=False)/0.5)

coords = np.concatenate((np.expand_dims(X.flatten(), 0),
                         np.expand_dims(Y.flatten(), 0),
                         np.expand_dims(Z.flatten(), 0)), 0)

gaussian_coords = gaussian(coords)

color_scale = lambda x: 'rgba(255, 0, 0, {})'.format(np.round(x, 2))

half = int(20**3/2)

trace1 = go.Scatter3d(
    x=X.flatten()[:half],
    y=Y.flatten()[:half],
    z=Z.flatten()[:half],
    mode='markers',
    marker=dict(
        size=10,
        color = list(map(color_scale, 
                         gaussian_coords.flatten().tolist()[:half])),
        line=dict(width=0, color='rgba(0, 0, 0, 0)'),
        # opacity=0.5  # Toggle this to see the Gaussian shape more clearly
    )
)

data=[trace1]

layout=go.Layout(height=600, width=600)

fig=go.Figure(data=data, layout=layout)
off.iplot(fig)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions