In Plotly, how to choose a specific color for legend points when using colorscale in group?

Viewed 261

I am plotting a 3d scatter plot in Plotly, similar to the example below. (Though in the actual plot, I have more data groups I'm plotting in various layers, so there's multiple entries in the legend.) I have to add color from a colorscale to each group of points. This has the unfortunate side effect of causing the color in the scatter point in the legend to be essentially a random color from this colorscale, rather than a specified one (on the darker end, for example). To see this effect, change the number in the _ = nprand.sample(1) line a few times and you should see the shade of blue in the legend point change.

import plotly.graph_objects as go
import numpy as np
import numpy.random as nprand

nprand.seed(1)
x = nprand.sample(100)
y = nprand.sample(100)
z = nprand.sample(100)
_ = nprand.sample(1)
color = nprand.sample(100)

trace = go.Scatter3d(x=x,
                     y=y,
                     z=z,
                     name="sample",
                     mode='markers',
                     marker=dict(
                         symbol='circle',
                         size=20,
                         color=color,
                         colorscale="Blues"))

fig = go.Figure(trace)
fig.update_layout(showlegend=True, legend=dict(bordercolor='black', itemwidth=100))
fig.show()

How can I specify a specific color for the point in the legend?

1 Answers

Generally, the title and color of the legend elements reflect the properties of the traces they represent. So if you've got a blue trace, you'll get a blue legend. In the case you're describing here, the color of the legend varies because the color associated with a specific value varies. And that particucal value seems to be the first element of:

fig.data[0].marker.color

Example content:

array([0.8118587 , 0.87496164, 0.68841325, 0.56949441, 0.16097144,
       0.46688002, 0.34517205, 0.22503996, 0.59251187, 0.31226984,
       0.91630555, 0.90963552, 0.25711829, 0.1108913 , 0.19296273,
       0.49958417, 0.72858567, 0.20819444, 0.24803356, 0.85167187,
       0.41584872, 0.61668507, 0.23366614, 0.10196726, 0.51585702,
       0.47714099, 0.15267164, 0.62180623, 0.54401012, 0.65413735,
       0.14454554,

So in your case, you can set the first element to 1 to get a darker color in the legend. Or 0 to get a lighter color in the legend. There are of course plenty of downsides to an approach like this, but this is how it works.

Dark fig.data[0].marker.color[0] = 1

enter image description here

Light fig.data[0].marker.color[0] = 0

enter image description here

Complete code:

import plotly.graph_objects as go
import numpy as np
import numpy.random as nprand

nprand.seed(1)
x = nprand.sample(100)
y = nprand.sample(100)
z = nprand.sample(100)
# _ = nprand.sample(1)
color = nprand.sample(100)

trace = go.Scatter3d(x=x,
                     y=y,
                     z=z,
                     name="sample",
                     mode='markers',
                     marker=dict(
                         symbol='circle',
                         size=20,
                         color=color,
                         colorscale="Blues"))

fig = go.Figure(trace)
fig.update_layout(showlegend=True, legend=dict(bordercolor='black', itemwidth=100))
fig.data[0].marker.color[0] = 0

fig.show()
Related