Hi @spencerchad,
The marker.color
property currently needs to be set to a number (though hopefully this will be more flexible in the future. see https://github.com/plotly/plotly.js/issues/1747). So the best approach for now is to customize the colorbar tick labels. Here’s a basic example
import plotly.graph_objs as go
import numpy as np
N = 100
color_names = ['A', 'B', 'C', 'D']
color_vals = list(range(len(color_names)))
num_colors = len(color_vals)
xs = np.random.rand(N)
ys = np.random.rand(N)
colors = np.arange(N) % num_colors
fig = go.FigureWidget(data=[
{'type': 'scatter',
'mode': 'markers',
'x': xs,
'y': ys,
'marker': {'color': colors,
'size': 10,
'colorbar': {
'tickvals': color_vals,
'ticktext': color_names
}
}}
])
fig
If you do a little extra work on the colorscale and colorbar, you can make something that looks more like a categorical legend:
import plotly.graph_objs as go
import numpy as np
N = 100
color_names = ['A', 'B', 'C', 'D']
color_vals = list(range(len(color_names)))
colors = ['red', 'green', 'blue', 'orange']
num_colors = len(color_vals)
xs = np.random.rand(N)
ys = np.random.rand(N)
colors = np.arange(N) % num_colors
colorscale = [
[0, 'red'],
[0.25, 'red'],
[0.25, 'green'],
[0.5, 'green'],
[0.5, 'blue'],
[0.75, 'blue'],
[0.75, 'orange'],
[1, 'orange']]
cmin = -0.5
cmax = num_colors - 0.5
colorscale
fig = go.FigureWidget(data=[
{'type': 'scatter',
'mode': 'markers',
'x': xs,
'y': ys,
'marker': {'color': colors,
'size': 10,
'colorbar': {
'tickvals': color_vals,
'ticktext': color_names,
'y': 1,
'len': 0.4,
'yanchor': 'top'
},
'colorscale': colorscale,
'cmin': cmin,
'cmax': cmax
}}
])
fig
Hope that helps!
-Jon